-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from autentia/feature/new-login
Feature/new login
- Loading branch information
Showing
17 changed files
with
254 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { inject, singleton } from 'tsyringe' | ||
import { AUTH_REPOSITORY, USER_REPOSITORY } from '../../../shared/di/container-tokens' | ||
import { Query, UseCaseKey } from '@archimedes/arch' | ||
import { UserCredentials } from '../domain/user-credentials' | ||
import { User } from '../../shared/user/domain/user' | ||
import type { AuthRepository } from '../domain/auth-repository' | ||
import type { UserRepository } from '../../shared/user/domain/user-repository' | ||
|
||
@UseCaseKey('LoginAndCheckUser') | ||
@singleton() | ||
export class LoginAndCheckUser extends Query<User, UserCredentials> { | ||
constructor( | ||
@inject(AUTH_REPOSITORY) private authRepository: AuthRepository, | ||
@inject(USER_REPOSITORY) private userRepository: UserRepository | ||
) { | ||
super() | ||
} | ||
|
||
async internalExecute(userCredentials: UserCredentials): Promise<User> { | ||
await this.authRepository.login(userCredentials) | ||
return this.userRepository.getUser() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { mock } from 'jest-mock-extended' | ||
import { AuthRepository } from '../domain/auth-repository' | ||
import { LoginCmd } from './login-cmd' | ||
|
||
describe('LoginCmd', () => { | ||
it('should execute login using the repository', async () => { | ||
const { userRepository, loginCmd } = setup() | ||
|
||
await loginCmd.internalExecute({ username: 'foo', password: 'password' }) | ||
|
||
expect(userRepository.login).toHaveBeenCalledWith({ username: 'foo', password: 'password' }) | ||
}) | ||
}) | ||
|
||
function setup() { | ||
const userRepository = mock<AuthRepository>() | ||
return { | ||
userRepository, | ||
loginCmd: new LoginCmd(userRepository) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Command, UseCaseKey } from '@archimedes/arch' | ||
import { inject, singleton } from 'tsyringe' | ||
import { AUTH_REPOSITORY } from '../../../shared/di/container-tokens' | ||
import type { AuthRepository } from '../domain/auth-repository' | ||
import { UserCredentials } from '../domain/user-credentials' | ||
|
||
@UseCaseKey('LoginCmd') | ||
@singleton() | ||
export class LoginCmd extends Command<UserCredentials> { | ||
constructor(@inject(AUTH_REPOSITORY) private userRepository: AuthRepository) { | ||
super() | ||
} | ||
|
||
async internalExecute(userCredentials: UserCredentials): Promise<void> { | ||
await this.userRepository.login(userCredentials) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import { UserCredentials } from './user-credentials' | ||
|
||
export interface AuthRepository { | ||
logout(): Promise<void> | ||
|
||
login(credentials: UserCredentials): Promise<void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface UserCredentials { | ||
username: string | ||
password: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
import { HttpClient } from '../../../shared/http/http-client' | ||
import { singleton } from 'tsyringe' | ||
import { AuthRepository } from '../domain/auth-repository' | ||
import { UserCredentials } from '../domain/user-credentials' | ||
|
||
@singleton() | ||
export class HttpAuthRepository implements AuthRepository { | ||
protected static logoutPath = '/logout' | ||
protected static loginPath = '/login' | ||
|
||
constructor(private httpClient: HttpClient) {} | ||
|
||
async logout(): Promise<void> { | ||
return this.httpClient.post<void>(HttpAuthRepository.logoutPath) | ||
} | ||
|
||
async login({ username, password }: UserCredentials): Promise<void> { | ||
return this.httpClient.post(HttpAuthRepository.loginPath, { username, password }) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/features/auth/ui/components/login-form/login-form.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as yup from 'yup' | ||
import { i18n } from '../../../../../shared/i18n/i18n' | ||
|
||
export interface LoginFormSchema { | ||
username: string | ||
password: string | ||
} | ||
|
||
export const loginFormSchema: yup.ObjectSchema<LoginFormSchema> = yup | ||
.object() | ||
.shape({ | ||
username: yup.string().required(i18n.t('form_errors.field_required')), | ||
password: yup.string().required(i18n.t('form_errors.field_required')) | ||
}) | ||
.defined() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { FormControl, FormErrorMessage, InputProps } from '@chakra-ui/react' | ||
import { forwardRef } from 'react' | ||
import { FloatingLabelInput } from '../floating-label-input' | ||
|
||
interface Props extends InputProps { | ||
label: string | ||
error?: string | ||
} | ||
|
||
export const PasswordField = forwardRef<HTMLInputElement, Props>((props, ref) => { | ||
const id = props.name + '_field' | ||
|
||
return ( | ||
<FormControl data-testid={id} id={id} isInvalid={props.error !== undefined}> | ||
<FloatingLabelInput type="password" ref={ref} {...props} /> | ||
<FormErrorMessage>{props.error}</FormErrorMessage> | ||
</FormControl> | ||
) | ||
}) | ||
|
||
PasswordField.displayName = 'PasswordField' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters