chore: better Prettier config for easier reviews

This commit is contained in:
2023-10-23 23:33:39 +02:00
parent a8781724d4
commit 71ea41695f
209 changed files with 4093 additions and 4114 deletions

View File

@ -1,14 +1,14 @@
import type { AxiosInstance } from 'axios'
import axios from 'axios'
import type { Socket } from 'socket.io-client'
import { io } from 'socket.io-client'
import { isUnauthorizedError } from '@thream/socketio-jwt'
import type { AxiosInstance } from "axios"
import axios from "axios"
import type { Socket } from "socket.io-client"
import { io } from "socket.io-client"
import { isUnauthorizedError } from "@thream/socketio-jwt"
import { API_URL } from '../api'
import { cookies } from '../cookies'
import type { Tokens } from '.'
import { fetchRefreshToken } from './authenticationFromServerSide'
import { clearCache } from '../cache'
import { API_URL } from "../api"
import { cookies } from "../cookies"
import type { Tokens } from "."
import { fetchRefreshToken } from "./authenticationFromServerSide"
import { clearCache } from "../cache"
export class Authentication {
public tokens: Tokens
@ -19,18 +19,20 @@ export class Authentication {
constructor(tokens: Tokens, disableSocketIO: boolean = false) {
this.tokens = tokens
this.accessTokenAge = Date.now()
if (typeof window === 'undefined' || disableSocketIO) {
if (typeof window === "undefined" || disableSocketIO) {
this.socket = undefined
} else {
this.socket = io(API_URL, {
auth: { token: `Bearer ${tokens.accessToken}` }
auth: { token: `Bearer ${tokens.accessToken}` },
})
this.socket.on('connect', () => {
this.socket.on("connect", () => {
console.log(
`Connected to socket with clientId: ${this.socket?.id ?? 'undefined'}`
`Connected to socket with clientId: ${
this.socket?.id ?? "undefined"
}`,
)
})
this.socket.on('connect_error', (error) => {
this.socket.on("connect_error", (error) => {
if (isUnauthorizedError(error)) {
fetchRefreshToken(this.tokens.refreshToken)
.then(({ accessToken }) => {
@ -46,8 +48,8 @@ export class Authentication {
this.api = axios.create({
baseURL: API_URL,
headers: {
'Content-Type': 'application/json'
}
"Content-Type": "application/json",
},
})
this.api.interceptors.request.use(
async (config) => {
@ -55,7 +57,7 @@ export class Authentication {
this.accessTokenAge + tokens.expiresIn > Date.now()
if (!isValidAccessToken) {
const { accessToken } = await fetchRefreshToken(
this.tokens.refreshToken
this.tokens.refreshToken,
)
this.setAccessToken(accessToken)
}
@ -65,7 +67,7 @@ export class Authentication {
async (error) => {
this.signout()
return await Promise.reject(error)
}
},
)
this.api.interceptors.response.use(
(response) => {
@ -78,7 +80,7 @@ export class Authentication {
error.config._retry = true
try {
const { accessToken } = await fetchRefreshToken(
this.tokens.refreshToken
this.tokens.refreshToken,
)
this.setAccessToken(accessToken)
error.response.config.headers.Authorization = `${this.tokens.type} ${this.tokens.accessToken}`
@ -87,7 +89,7 @@ export class Authentication {
this.signout()
return await Promise.reject(error)
}
}
},
)
}
@ -95,21 +97,21 @@ export class Authentication {
this.tokens.accessToken = accessToken
this.accessTokenAge = Date.now()
const token = `${this.tokens.type} ${this.tokens.accessToken}`
if (typeof this?.socket?.auth !== 'function' && this.socket != null) {
this.socket.auth['token'] = token
if (typeof this?.socket?.auth !== "function" && this.socket != null) {
this.socket.auth["token"] = token
}
}
public signout(): void {
cookies.remove('refreshToken')
cookies.remove("refreshToken")
clearCache()
window.location.href = '/authentication/signin'
window.location.href = "/authentication/signin"
}
public async signoutServerSide(): Promise<void> {
try {
await this.api.post('/users/signout', {
refreshToken: this.tokens.refreshToken
await this.api.post("/users/signout", {
refreshToken: this.tokens.refreshToken,
})
} catch {}
this.signout()
@ -117,13 +119,13 @@ export class Authentication {
public async signoutAllDevicesServerSide(): Promise<void> {
try {
await this.api.delete('/users/signout')
await this.api.delete("/users/signout")
} catch {}
this.signout()
}
public signin(): void {
clearCache()
cookies.set('refreshToken', this.tokens.refreshToken)
cookies.set("refreshToken", this.tokens.refreshToken)
}
}

View File

@ -1,11 +1,11 @@
import { createContext, useState, useEffect, useMemo, useContext } from 'react'
import { useTheme } from 'next-themes'
import setLanguage from 'next-translate/setLanguage'
import useTranslation from 'next-translate/useTranslation'
import { createContext, useState, useEffect, useMemo, useContext } from "react"
import { useTheme } from "next-themes"
import setLanguage from "next-translate/setLanguage"
import useTranslation from "next-translate/useTranslation"
import type { PagePropsWithAuthentication } from '.'
import { Authentication } from '.'
import type { UserCurrent } from '../../models/User'
import type { PagePropsWithAuthentication } from "."
import { Authentication } from "."
import type { UserCurrent } from "../../models/User"
export interface AuthenticationValue {
authentication: Authentication
@ -15,7 +15,7 @@ export interface AuthenticationValue {
const defaultAuthenticationContext: AuthenticationValue = {} as any
const AuthenticationContext = createContext<AuthenticationValue>(
defaultAuthenticationContext
defaultAuthenticationContext,
)
export const AuthenticationProvider: React.FC<
@ -42,15 +42,15 @@ export const AuthenticationProvider: React.FC<
useEffect(() => {
authentication.api
.put('/users/current/settings', { theme, language: lang })
.put("/users/current/settings", { theme, language: lang })
.then(({ data: userCurrentSettings }) => {
setUser((oldUser) => {
return {
...oldUser,
settings: {
...oldUser.settings,
...userCurrentSettings.settings
}
...userCurrentSettings.settings,
},
}
})
})

View File

@ -1,20 +1,20 @@
import type { AxiosInstance, AxiosResponse } from 'axios'
import type { GetServerSideProps, GetServerSidePropsContext } from 'next'
import type { AxiosInstance, AxiosResponse } from "axios"
import type { GetServerSideProps, GetServerSidePropsContext } from "next"
import { api } from '../api'
import { Cookies } from '../cookies'
import type { RefreshTokenResponse, Tokens } from './index'
import { Authentication } from './Authentication'
import type { UserCurrent } from '../../models/User'
import { api } from "../api"
import { Cookies } from "../cookies"
import type { RefreshTokenResponse, Tokens } from "./index"
import { Authentication } from "./Authentication"
import type { UserCurrent } from "../../models/User"
export const fetchRefreshToken = async (
refreshToken: string
refreshToken: string,
): Promise<Tokens> => {
const { data } = await api.post<RefreshTokenResponse>(
'/users/refresh-token',
"/users/refresh-token",
{
refreshToken
}
refreshToken,
},
)
return { ...data, refreshToken }
}
@ -25,33 +25,33 @@ interface AuthenticationFromServerSideOptions {
/** 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?: (
context: GetServerSidePropsContext,
api: AxiosInstance
api: AxiosInstance,
) => Promise<{ [key: string]: any }>
}
export const authenticationFromServerSide = (
options: AuthenticationFromServerSideOptions
options: AuthenticationFromServerSideOptions,
): GetServerSideProps => {
const { shouldBeAuthenticated, fetchData } = options
return async (context) => {
const cookies = new Cookies(context.req.headers.cookie)
const refreshToken = cookies.get('refreshToken')
const refreshToken = cookies.get("refreshToken")
let tokens: Tokens | null = null
if (refreshToken != null) {
try {
tokens = await fetchRefreshToken(refreshToken)
} catch {
cookies.remove('refreshToken')
cookies.remove("refreshToken")
}
}
if (!shouldBeAuthenticated) {
if (tokens != null) {
return {
redirect: {
destination: '/application',
permanent: false
}
destination: "/application",
permanent: false,
},
}
} else {
let data: any = {}
@ -67,9 +67,9 @@ export const authenticationFromServerSide = (
if (tokens == null) {
return {
redirect: {
destination: '/authentication/signin',
permanent: false
}
destination: "/authentication/signin",
permanent: false,
},
}
} else {
try {
@ -78,7 +78,7 @@ export const authenticationFromServerSide = (
const { data: currentUser } = await authentication.api.get<
unknown,
AxiosResponse<UserCurrent>
>('/users/current')
>("/users/current")
if (fetchData != null) {
data = await fetchData(context, authentication.api)
}
@ -86,11 +86,11 @@ export const authenticationFromServerSide = (
return data
}
return {
props: { authentication: { tokens, ...currentUser }, ...data }
props: { authentication: { tokens, ...currentUser }, ...data },
}
} catch {
return {
notFound: true
notFound: true,
}
}
}

View File

@ -1,11 +1,11 @@
import type { UserCurrent } from '../../models/User'
import type { UserCurrent } from "../../models/User"
export interface RefreshTokenResponse {
accessToken: string
/** how long, in milliseconds, until the accessToken expires */
expiresIn: number
type: 'Bearer'
type: "Bearer"
}
export interface Tokens extends RefreshTokenResponse {
@ -21,13 +21,13 @@ export interface PagePropsWithAuthentication {
export const isTokens = (data: { [key: string]: any }): data is Tokens => {
return (
'accessToken' in data &&
'refreshToken' in data &&
'type' in data &&
'expiresIn' in data
"accessToken" in data &&
"refreshToken" in data &&
"type" in data &&
"expiresIn" in data
)
}
export * from './Authentication'
export * from './authenticationFromServerSide'
export * from './AuthenticationContext'
export * from "./Authentication"
export * from "./authenticationFromServerSide"
export * from "./AuthenticationContext"