-
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 #86 from autentia/feature/#177848857-sign-in-with-…
…google-and-micronaut Feature/#177848857 sign in with google and micronaut
- Loading branch information
Showing
37 changed files
with
243 additions
and
726 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
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
src/modules/login/components/PasswordField/PasswordField.tsx
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
src/modules/login/components/PasswordField/PasswordInput.test.tsx
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
src/modules/login/components/PasswordField/PasswordInput.tsx
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
src/modules/login/components/SignInWithGoogle/GoogleIcon.tsx
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,10 @@ | ||
import { IconProps } from '@chakra-ui/icons' | ||
import { Icon } from '@chakra-ui/react' | ||
|
||
export const GoogleIcon: React.FC<IconProps> = (props) => { | ||
return ( | ||
<Icon viewBox="0 0 488 512" {...props}> | ||
<path d="M488 261.8C488 403.3 391.1 504 248 504 110.8 504 0 393.2 0 256S110.8 8 248 8c66.8 0 123 24.5 166.3 64.9l-67.5 64.9C258.5 52.6 94.3 116.6 94.3 256c0 86.5 69.1 156.6 153.7 156.6 98.2 0 135-70.4 140.8-106.9H248v-85.3h236.1c2.3 12.7 3.9 24.9 3.9 41.4z" /> | ||
</Icon> | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
src/modules/login/components/SignInWithGoogle/SignInWithGoogleButton.tsx
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,24 @@ | ||
import { Button } from '@chakra-ui/react' | ||
import { useTranslation } from 'react-i18next' | ||
import endpoints from 'shared/api/endpoints' | ||
import { GoogleIcon } from './GoogleIcon' | ||
|
||
export const SignInWithGoogleButton: React.FC = () => { | ||
const { t } = useTranslation() | ||
|
||
const onClick = () => { | ||
window.location.assign(endpoints.googleLogin) | ||
} | ||
|
||
return ( | ||
<Button | ||
type="button" | ||
colorScheme="brand" | ||
variant="solid" | ||
onClick={onClick} | ||
leftIcon={<GoogleIcon fill="white" boxSize={4} mr={4} />} | ||
> | ||
{t('login_page.sign_in_with_google')} | ||
</Button> | ||
) | ||
} |
54 changes: 54 additions & 0 deletions
54
src/modules/login/data-access/actions/auto-login-action.test.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,54 @@ | ||
import { AppState } from 'shared/data-access/state/app-state' | ||
import { container } from 'tsyringe' | ||
import { mock } from 'jest-mock-extended' | ||
import { UserRepository } from '../repositories/user-repository' | ||
import { AutoLoginAction } from './auto-login-action' | ||
import { buildUser } from 'test-utils/generateTestMocks' | ||
import { AnonymousUserError } from '../errors/anonymous-user-error' | ||
|
||
describe('AutoLoginAction', () => { | ||
test('should set loggedUser state when repository return the user', async () => { | ||
const { appState, userRepository, autoLoginAction } = setup() | ||
userRepository.getUser.mockResolvedValue(buildUser()) | ||
appState.loggedUser = undefined | ||
|
||
await autoLoginAction.execute() | ||
|
||
expect(appState.loggedUser).toEqual(buildUser()) | ||
}) | ||
|
||
test('should set isAuthenticated state when repository return the user', async () => { | ||
const { appState, userRepository, autoLoginAction } = setup() | ||
userRepository.getUser.mockResolvedValue(buildUser()) | ||
appState.isAuthenticated = false | ||
|
||
await autoLoginAction.execute() | ||
|
||
expect(appState.isAuthenticated).toBeTruthy() | ||
}) | ||
|
||
test('should throw error when error is not AnonymousUserError', async () => { | ||
const { userRepository, autoLoginAction } = setup() | ||
userRepository.getUser.mockRejectedValue(new Error()) | ||
|
||
expect(autoLoginAction.execute()).rejects.toThrowError() | ||
}) | ||
|
||
test('should not throw error when error is AnonymousUserError', async () => { | ||
const { userRepository, autoLoginAction } = setup() | ||
userRepository.getUser.mockRejectedValue(new AnonymousUserError()) | ||
|
||
expect(autoLoginAction.execute()).resolves.not.toThrowError() | ||
}) | ||
}) | ||
|
||
function setup() { | ||
const userRepository = mock<UserRepository>() | ||
const appState = container.resolve(AppState) | ||
|
||
return { | ||
userRepository, | ||
appState, | ||
autoLoginAction: new AutoLoginAction(appState, userRepository) | ||
} | ||
} |
35 changes: 15 additions & 20 deletions
35
src/modules/login/data-access/actions/auto-login-action.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 |
---|---|---|
@@ -1,37 +1,32 @@ | ||
import { action, makeObservable, runInAction } from 'mobx' | ||
import { inject, singleton } from 'tsyringe' | ||
import { singleton } from 'tsyringe' | ||
import type { IAction } from 'shared/arch/interfaces/IAction' | ||
import type { TokenStorage } from 'shared/api/oauth/token-storage/token-storage' | ||
import { OAuthRepository } from 'shared/api/oauth/oauth-repository' | ||
import { AppState } from 'shared/data-access/state/app-state' | ||
import { UserRepository } from 'modules/login/data-access/repositories/user-repository' | ||
import { AnonymousUserError } from '../errors/anonymous-user-error' | ||
|
||
@singleton() | ||
export class AutoLoginAction implements IAction<{ username: string; password: string }> { | ||
constructor( | ||
@inject('TokenStorage') private tokenStorage: TokenStorage, | ||
private oauthRepository: OAuthRepository, | ||
private appState: AppState, | ||
private userRepository: UserRepository | ||
) { | ||
constructor(private appState: AppState, private userRepository: UserRepository) { | ||
makeObservable(this) | ||
} | ||
|
||
@action | ||
async execute(): Promise<void> { | ||
const refreshToken = await this.tokenStorage.getRefreshToken() | ||
|
||
if (refreshToken) { | ||
const oauthResponse = await this.oauthRepository.renewOAuthByRefreshToken(refreshToken) | ||
this.tokenStorage.setAccessToken(oauthResponse.access_token) | ||
await this.tokenStorage.setRefreshToken(oauthResponse.refresh_token) | ||
|
||
try { | ||
const user = await this.userRepository.getUser() | ||
|
||
runInAction(() => { | ||
this.appState.isAuthenticated = true | ||
this.appState.loggedUser = user | ||
}) | ||
if (user) { | ||
runInAction(() => { | ||
this.appState.isAuthenticated = true | ||
this.appState.loggedUser = user | ||
}) | ||
} | ||
} catch (error) { | ||
const isAnonymousUser = error instanceof AnonymousUserError | ||
if (!isAnonymousUser) { | ||
throw error | ||
} | ||
} | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
src/modules/login/data-access/actions/login-action.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.