-
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.
- Loading branch information
Showing
18 changed files
with
113 additions
and
78 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 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,19 +1,34 @@ | ||
// import { NextPageContext } from 'next' | ||
import { getTokenFromCookies, setTokenIntoCookies } from 'src/core/services' | ||
import { NextPageContext } from 'next' | ||
import { parseCookies, setCookie } from 'nookies' | ||
import { constants } from 'src/constants' | ||
import { createAnonymousUser } from 'src/core/services' | ||
import { IAuthUserTokenStorage } from 'src/core/useCases/authUser' | ||
|
||
export class CookiesAuthUserTokenStorage implements IAuthUserTokenStorage { | ||
// constructor(private nextContext: NextPageContext) {} | ||
static authToken = '' | ||
|
||
setToken(token: string) { | ||
setTokenIntoCookies(token) | ||
static async initToken(ctx?: NextPageContext): Promise<void> { | ||
const token = CookiesAuthUserTokenStorage.getTokenFromCookie(ctx) || await createAnonymousUser() | ||
CookiesAuthUserTokenStorage.setToken(token, ctx) | ||
} | ||
|
||
static getTokenFromCookie(ctx?: NextPageContext): string | null { | ||
return parseCookies(ctx)[constants.AUTH_TOKEN_KEY] | ||
} | ||
|
||
getToken() { | ||
return getTokenFromCookies() | ||
static setToken(authToken: string, ctx?: NextPageContext): void { | ||
setCookie(ctx, constants.AUTH_TOKEN_KEY, authToken, { | ||
maxAge: constants.AUTH_TOKEN_TTL, | ||
path: '/', | ||
}) | ||
CookiesAuthUserTokenStorage.authToken = authToken | ||
} | ||
|
||
setToken(token: string) { | ||
CookiesAuthUserTokenStorage.setToken(token) | ||
} | ||
|
||
removeToken() { | ||
this.setToken('') | ||
getToken(ctx?: NextPageContext) { | ||
return CookiesAuthUserTokenStorage.getTokenFromCookie(ctx) | ||
} | ||
} |
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,11 @@ | ||
import { useCallback } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import { setTokenIntoCookies, createAnonymousUser } from 'src/core/services' | ||
import { authUserActions } from 'src/core/useCases/authUser' | ||
|
||
export function useOnClickLogout() { | ||
const dispatch = useDispatch() | ||
|
||
return useCallback(async () => { | ||
setTokenIntoCookies('') | ||
await createAnonymousUser() | ||
dispatch(authUserActions.setUser(null)) | ||
dispatch(authUserActions.logout()) | ||
}, [dispatch]) | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,15 +1,9 @@ | ||
import { NextPageContext } from 'next' | ||
import { api } from 'src/core/api' | ||
import { setTokenIntoCookies } from './authToken' | ||
|
||
export async function createAnonymousUser(ctx?: NextPageContext): Promise<string> { | ||
export async function createAnonymousUser(): Promise<string> { | ||
const anonymousUsersRes = await api.request({ | ||
name: '/anonymous_users POST', | ||
}) | ||
|
||
const anonymousToken = anonymousUsersRes.data.user.token | ||
|
||
setTokenIntoCookies(anonymousToken, ctx) | ||
|
||
return anonymousToken | ||
return anonymousUsersRes.data.user.token | ||
} |
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,2 +1 @@ | ||
export * from './authToken' | ||
export * from './createAnonymousUser' |
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,5 +1,4 @@ | ||
export interface IAuthUserTokenStorage { | ||
getToken(): string | null; | ||
setToken(token: string): void; | ||
removeToken(): 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
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
Oops, something went wrong.