Skip to content

Commit

Permalink
auth (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiseek authored Oct 1, 2023
1 parent 913af79 commit 5392bb5
Show file tree
Hide file tree
Showing 298 changed files with 21,912 additions and 9,679 deletions.
2 changes: 2 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ DB_USER=postgres
DB_PASS=postgres
DB_NAME=database

JWT_SECRET='DO NOT USE THIS VALUE. INSTEAD, CREATE A COMPLEX SECRET AND KEEP IT SAFE OUTSIDE OF THE SOURCE CODE.'

PORT=3000
25 changes: 25 additions & 0 deletions account/data-access/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
11 changes: 11 additions & 0 deletions account/data-access/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# account-data-access

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build account-data-access` to build the library.

## Running unit tests

Run `nx test account-data-access` to execute the unit tests via [Jest](https://jestjs.io).
10 changes: 10 additions & 0 deletions account/data-access/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'account-data-access',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', {tsconfig: '<rootDir>/tsconfig.spec.json'}],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/account/data-access',
}
14 changes: 14 additions & 0 deletions account/data-access/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@dev/account-data-access",
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0",
"@dev/account-domain": "*",
"@dev/shared-data-access": "*",
"rxjs": "^7.8.0",
"@dev/shared-util-data": "*"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
43 changes: 43 additions & 0 deletions account/data-access/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "account-data-access",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "account/data-access/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/account/data-access",
"main": "account/data-access/src/index.ts",
"tsConfig": "account/data-access/tsconfig.lib.json",
"assets": ["account/data-access/*.md"]
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"account/data-access/**/*.ts",
"account/data-access/package.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "account/data-access/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": ["type:data-access", "scope:account", "side:client"]
}
1 change: 1 addition & 0 deletions account/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public-api'
53 changes: 53 additions & 0 deletions account/data-access/src/lib/application/auth.facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {AuthService, AuthUser, CreateUser, SignIn} from '@dev/account-domain'
import {Store} from '@dev/shared-data-access'
import {switchMap, take, tap} from 'rxjs'

interface AuthState {
authUser: AuthUser | null
warning: string | null
loading: boolean
}
export class AuthFacade extends Store<AuthState> {
authUser$ = this.select((state) => state.authUser)
warning$ = this.select((state) => state.warning)
loading$ = this.select((state) => state.loading)

get accessToken() {
return this.authService.accessToken
}

constructor(private authService: AuthService) {
super({authUser: null, warning: null, loading: false})
}

login<T extends SignIn>(signIn: T) {
this.setState({loading: true})
const authUser$ = this.authService.login(signIn).pipe(
switchMap(() => this.authService.me()),
take(1)
)

authUser$.subscribe((authUser) => this.setState({authUser, loading: false}))
}

me() {
return this.authService.me().pipe(
tap((authUser) => this.setState({authUser})),
take(1)
)
}

register(createUser: CreateUser) {
this.setState({loading: true})
const createUser$ = this.authService.register(createUser).pipe(take(1))

createUser$.subscribe((authUser) =>
this.setState({authUser, loading: false})
)
}

logout() {
this.authService.accessToken = null
this.setState({authUser: null})
}
}
4 changes: 4 additions & 0 deletions account/data-access/src/lib/application/group.facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Facade} from '@dev/shared-util-data'
import {Group} from '@dev/account-domain'

export class GroupFacade extends Facade<Group> {}
3 changes: 3 additions & 0 deletions account/data-access/src/lib/application/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './auth.facade'
export * from './group.facade'
export * from './user.facade'
17 changes: 17 additions & 0 deletions account/data-access/src/lib/application/user.facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {UpdatePassword, User, UserService} from '@dev/account-domain'
import {Facade} from '@dev/shared-util-data'
import {catchError} from 'rxjs'

export class UserFacade extends Facade<User, UserService> {
constructor(service: UserService) {
super(service)
}

updatePassword(value: UpdatePassword) {
const update$ = this.service.updatePassword(value)
update$.pipe(catchError(this.handleError)).subscribe((selected) => {
this.setState({selected})
this.findOne(value.id)
})
}
}
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/create-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {CreateGroup} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/create-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {CreateUser} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {Group} from '@dev/account-domain'
8 changes: 8 additions & 0 deletions account/data-access/src/lib/dtos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './create-group'
export * from './create-user'
export * from './group'
export * from './sign-in'
export * from './update-group'
export * from './update-password'
export * from './update-user'
export * from './user'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/sign-in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {SignIn} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/update-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {UpdateGroup} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/update-password.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {UpdatePassword} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/update-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {UpdateUser} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {User} from '@dev/account-domain'
36 changes: 36 additions & 0 deletions account/data-access/src/lib/infrastructure/auth.service.impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
SignIn,
AuthUser,
AccessToken,
AuthService,
CreateUser,
} from '@dev/account-domain'
import {Http} from '@dev/shared-data-access'
import {take, tap} from 'rxjs'

export class AuthServiceImpl implements AuthService {
get accessToken() {
return localStorage.getItem('accessToken')
}
set accessToken(value) {
if (value) localStorage.setItem('accessToken', value)
else localStorage.removeItem('accessToken')
}

constructor(private readonly http: Http, readonly url: string) {}

login(value: SignIn) {
return this.http.post<AccessToken>(`${this.url}/login`, value).pipe(
tap(({accessToken}) => (this.accessToken = accessToken)),
take(1)
)
}

register(value: CreateUser) {
return this.http.post<AuthUser>(`${this.url}/register`, value).pipe(take(1))
}

me() {
return this.http.get<AuthUser>(`${this.url}/me`).pipe(take(1))
}
}
33 changes: 33 additions & 0 deletions account/data-access/src/lib/infrastructure/auth.service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {AuthService, CreateUser, SignIn} from '@dev/account-domain'
import {of} from 'rxjs'

const MOCK_USER = {
name: 'mock',
username: 'username',
email: '[email protected]',
id: 'mo-ck-id',
}
const MOCK_AUTH = `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1YjU2YTlkNy0wMWMwLTQ4NjEtYmY0MC1jMzMwNjMzY2E3MWQiLCJuYW1lIjoiR3VpbGhlcm1lIiwidXNlcm5hbWUiOiJndWlzZWVrIiwiZW1haWwiOiJlbWFpbEBndWlzZWVrLmRldiIsImlhdCI6MTY5NjEzMzMzMywiZXhwIjoxNjk2MjE5NzMzfQ.SyeK25DlNIJNl3eu8Jabjd0XaQWm-j_WSB8f5MoYEGE`

export class AuthServiceMock implements AuthService {
get accessToken() {
return localStorage.getItem('accessToken')
}
set accessToken(value) {
if (value) localStorage.setItem('accessToken', value)
else localStorage.removeItem('accessToken')
}

login(value: SignIn) {
console.log(`login: `, value)
return of({accessToken: MOCK_AUTH})
}
register(value: CreateUser) {
console.log(`register: `, value)
return of(MOCK_USER)
}

me() {
return of(MOCK_USER)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {Group, GroupService} from '@dev/account-domain'
import {BaseService} from '@dev/shared-data-access'

export class GroupServiceImpl
extends BaseService<Group>
implements GroupService {}
19 changes: 19 additions & 0 deletions account/data-access/src/lib/infrastructure/group.service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Group, GroupService} from '@dev/account-domain'
import {MockService} from '@dev/shared-data-access'
import {of} from 'rxjs'

export class GroupServiceMock
extends MockService<Group>
implements GroupService
{
create(value: Partial<Group>) {
const entity = {
...value,
id: crypto.randomUUID(),
createdAt: new Date(),
updatedAt: new Date(),
} as Group
this.collection.push(entity)
return of(entity)
}
}
6 changes: 6 additions & 0 deletions account/data-access/src/lib/infrastructure/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './auth.service.impl'
export * from './auth.service.mock'
export * from './group.service.impl'
export * from './group.service.mock'
export * from './user.service.impl'
export * from './user.service.mock'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {UpdatePassword, User, UserService} from '@dev/account-domain'
import {BaseService} from '@dev/shared-data-access'
import {Observable} from 'rxjs'

export class UserServiceImpl extends BaseService<User> implements UserService {
updatePassword(value: UpdatePassword): Observable<User> {
return this.http.put(`${this.url}/password`, value)
}
}
20 changes: 20 additions & 0 deletions account/data-access/src/lib/infrastructure/user.service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {UpdatePassword, User, UserService} from '@dev/account-domain'
import {MockService} from '@dev/shared-data-access'
import {of} from 'rxjs'

export class UserServiceMock extends MockService<User> implements UserService {
updatePassword(value: UpdatePassword) {
return this.findOne(value.id)
}

create(value: Partial<User>) {
const entity = {
...value,
id: crypto.randomUUID(),
createdAt: new Date(),
updatedAt: new Date(),
} as User
this.collection.push(entity)
return of(entity)
}
}
Loading

0 comments on commit 5392bb5

Please sign in to comment.