chore: initial commit
This commit is contained in:
105
utils/authentication/Authentication.ts
Normal file
105
utils/authentication/Authentication.ts
Normal file
@ -0,0 +1,105 @@
|
||||
import axios, { AxiosInstance } from 'axios'
|
||||
import { io, Socket } from 'socket.io-client'
|
||||
|
||||
import { API_URL } from 'utils/api'
|
||||
import { cookies } from 'utils/cookies'
|
||||
import { Tokens } from '.'
|
||||
import { fetchRefreshToken } from './authenticationFromServerSide'
|
||||
|
||||
export class Authentication {
|
||||
public tokens: Tokens
|
||||
public accessTokenAge: number
|
||||
public socket: Socket
|
||||
public api: AxiosInstance
|
||||
|
||||
constructor (tokens: Tokens) {
|
||||
this.tokens = tokens
|
||||
this.accessTokenAge = Date.now()
|
||||
this.socket = io(API_URL, {
|
||||
auth: { token: `Bearer ${tokens.accessToken}` }
|
||||
})
|
||||
this.socket.on('connect_error', (error) => {
|
||||
if (error.message.startsWith('Unauthorized')) {
|
||||
fetchRefreshToken(this.tokens.refreshToken)
|
||||
.then(({ accessToken }) => {
|
||||
this.setAccessToken(accessToken)
|
||||
})
|
||||
.catch(async () => {
|
||||
this.signout()
|
||||
return await Promise.reject(error)
|
||||
})
|
||||
}
|
||||
})
|
||||
this.api = axios.create({
|
||||
baseURL: API_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
this.api.interceptors.request.use(
|
||||
async (config) => {
|
||||
const isValidAccessToken =
|
||||
this.accessTokenAge + tokens.expiresIn > Date.now()
|
||||
if (!isValidAccessToken) {
|
||||
const { accessToken } = await fetchRefreshToken(
|
||||
this.tokens.refreshToken
|
||||
)
|
||||
this.setAccessToken(accessToken)
|
||||
}
|
||||
config.headers.Authorization = `${this.tokens.type} ${this.tokens.accessToken}`
|
||||
return config
|
||||
},
|
||||
async (error) => {
|
||||
this.signout()
|
||||
return await Promise.reject(error)
|
||||
}
|
||||
)
|
||||
this.api.interceptors.response.use(
|
||||
(response) => {
|
||||
return response
|
||||
},
|
||||
async (error) => {
|
||||
if (error.response.status !== 403 || (error.config._retry as boolean)) {
|
||||
return await Promise.reject(error)
|
||||
}
|
||||
error.config._retry = true
|
||||
try {
|
||||
const { accessToken } = await fetchRefreshToken(
|
||||
this.tokens.refreshToken
|
||||
)
|
||||
this.setAccessToken(accessToken)
|
||||
error.response.config.headers.Authorization = `${this.tokens.type} ${this.tokens.accessToken}`
|
||||
return await this.api.request(error.response.config)
|
||||
} catch {
|
||||
this.signout()
|
||||
return await Promise.reject(error)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
public setAccessToken (accessToken: string): void {
|
||||
this.tokens.accessToken = accessToken
|
||||
this.accessTokenAge = Date.now()
|
||||
const token = `${this.tokens.type} ${this.tokens.accessToken}`
|
||||
if (typeof this.socket.auth !== 'function') {
|
||||
this.socket.auth.token = token
|
||||
}
|
||||
}
|
||||
|
||||
public signout (): void {
|
||||
cookies.remove('refreshToken')
|
||||
window.location.href = '/authentication/signin'
|
||||
}
|
||||
|
||||
public async signoutServerSide (): Promise<void> {
|
||||
await this.api.post('/users/signout', {
|
||||
refreshToken: this.tokens.refreshToken
|
||||
})
|
||||
this.signout()
|
||||
}
|
||||
|
||||
public signin (): void {
|
||||
cookies.set('refreshToken', this.tokens.refreshToken)
|
||||
}
|
||||
}
|
60
utils/authentication/AuthenticationContext.tsx
Normal file
60
utils/authentication/AuthenticationContext.tsx
Normal file
@ -0,0 +1,60 @@
|
||||
import { createContext, useState, useEffect, useMemo, useContext } from 'react'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import setLanguage from 'next-translate/setLanguage'
|
||||
|
||||
import {
|
||||
Authentication,
|
||||
PagePropsWithAuthentication,
|
||||
User,
|
||||
UserSettings
|
||||
} from '.'
|
||||
import { useTheme } from 'contexts/Theme'
|
||||
|
||||
export interface AuthenticationValue {
|
||||
authentication: Authentication
|
||||
user: User
|
||||
settings: UserSettings
|
||||
}
|
||||
|
||||
const defaultAnthenticationContext: AuthenticationValue = {} as any
|
||||
const AuthenticationContext = createContext<AuthenticationValue>(
|
||||
defaultAnthenticationContext
|
||||
)
|
||||
|
||||
export const AuthenticationProvider: React.FC<PagePropsWithAuthentication> = (
|
||||
props
|
||||
) => {
|
||||
const { lang } = useTranslation()
|
||||
const { theme, setTheme } = useTheme()
|
||||
const [user] = useState<User>(props.authentication.user)
|
||||
const [settings] = useState<UserSettings>(props.authentication.settings)
|
||||
|
||||
const authentication = useMemo(() => {
|
||||
return new Authentication(props.authentication.tokens)
|
||||
}, [props.authentication.tokens])
|
||||
|
||||
useEffect(() => {
|
||||
setLanguage(settings.language).catch(() => {})
|
||||
setTheme(settings.theme)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
authentication.api.put('/users/current/settings', { theme }).catch(() => {})
|
||||
}, [theme])
|
||||
|
||||
useEffect(() => {
|
||||
authentication.api
|
||||
.put('/users/current/settings', { language: lang })
|
||||
.catch(() => {})
|
||||
}, [lang])
|
||||
|
||||
return (
|
||||
<AuthenticationContext.Provider value={{ authentication, user, settings }}>
|
||||
{props.children}
|
||||
</AuthenticationContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useAuthentication = (): AuthenticationValue => {
|
||||
return useContext(AuthenticationContext)
|
||||
}
|
75
utils/authentication/authenticationFromServerSide.ts
Normal file
75
utils/authentication/authenticationFromServerSide.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { AxiosInstance } from 'axios'
|
||||
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
|
||||
|
||||
import { api } from 'utils/api'
|
||||
import { Cookies } from 'utils/cookies'
|
||||
import { RefreshTokenResponse, Tokens } from '.'
|
||||
import { Authentication } from './Authentication'
|
||||
|
||||
export const fetchRefreshToken = async (
|
||||
refreshToken: string
|
||||
): Promise<Tokens> => {
|
||||
const { data } = await api.post<RefreshTokenResponse>('/users/refreshToken', {
|
||||
refreshToken
|
||||
})
|
||||
return { ...data, refreshToken }
|
||||
}
|
||||
|
||||
interface AuthenticationFromServerSideOptions {
|
||||
shouldBeAuthenticated: boolean
|
||||
|
||||
/** allows to fetch data server side with the authenticated user, the callback should returns the data that will be transfer to the component as props */
|
||||
fetchData?: (api: AxiosInstance, context: GetServerSidePropsContext) => Promise<{ [key: string]: any }>
|
||||
}
|
||||
|
||||
export const authenticationFromServerSide = (
|
||||
options: AuthenticationFromServerSideOptions
|
||||
): GetServerSideProps => {
|
||||
const { shouldBeAuthenticated, fetchData } = options
|
||||
|
||||
return async (context) => {
|
||||
const cookies = new Cookies(context.req.headers.cookie)
|
||||
const refreshToken = cookies.get('refreshToken')
|
||||
let tokens: Tokens | null = null
|
||||
if (refreshToken != null) {
|
||||
try {
|
||||
tokens = await fetchRefreshToken(refreshToken)
|
||||
} catch {
|
||||
cookies.remove('refreshToken')
|
||||
}
|
||||
}
|
||||
if (!shouldBeAuthenticated) {
|
||||
if (tokens != null) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/application',
|
||||
permanent: false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return { props: {} }
|
||||
}
|
||||
} else {
|
||||
if (tokens == null) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/authentication/signin',
|
||||
permanent: false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let data = {}
|
||||
const authentication = new Authentication(tokens)
|
||||
const { data: currentUser } = await authentication.api.get(
|
||||
'/users/current'
|
||||
)
|
||||
if (fetchData != null) {
|
||||
data = await fetchData(authentication.api, context)
|
||||
}
|
||||
return {
|
||||
props: { authentication: { tokens, ...currentUser }, ...data }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
53
utils/authentication/index.ts
Normal file
53
utils/authentication/index.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import { Theme } from 'contexts/Theme'
|
||||
|
||||
export interface RefreshTokenResponse {
|
||||
accessToken: string
|
||||
|
||||
/** how long, in milliseconds, until the accessToken expires */
|
||||
expiresIn: number
|
||||
type: 'Bearer'
|
||||
}
|
||||
|
||||
export interface Tokens extends RefreshTokenResponse {
|
||||
refreshToken: string
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: number
|
||||
name: string
|
||||
email: string
|
||||
status: string
|
||||
biography: string
|
||||
logo: string
|
||||
isConfirmed: boolean
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
export const languages = ['en', 'fr'] as const
|
||||
export type Language = typeof languages[number]
|
||||
|
||||
export interface UserSettings {
|
||||
language: Language
|
||||
theme: Theme
|
||||
isPublicEmail: boolean
|
||||
}
|
||||
|
||||
export const providers = ['google', 'github', 'discord'] as const
|
||||
export const strategies = [...providers, 'local'] as const
|
||||
|
||||
export type AuthenticationStrategy = typeof strategies[number]
|
||||
|
||||
export interface PagePropsWithAuthentication {
|
||||
authentication: {
|
||||
tokens: Tokens
|
||||
user: User
|
||||
settings: UserSettings
|
||||
currentStrategy: AuthenticationStrategy
|
||||
strategies: AuthenticationStrategy[]
|
||||
}
|
||||
}
|
||||
|
||||
export * from './Authentication'
|
||||
export * from './authenticationFromServerSide'
|
||||
export * from './AuthenticationContext'
|
Reference in New Issue
Block a user