2021-10-24 06:09:43 +02:00
|
|
|
import axios, { AxiosInstance } from 'axios'
|
|
|
|
import { io, Socket } from 'socket.io-client'
|
|
|
|
|
2021-10-26 16:38:55 +02:00
|
|
|
import { API_URL } from '../api'
|
|
|
|
import { cookies } from '../cookies'
|
2022-01-01 20:42:25 +01:00
|
|
|
import { Tokens } from '.'
|
2021-10-24 06:09:43 +02:00
|
|
|
import { fetchRefreshToken } from './authenticationFromServerSide'
|
|
|
|
|
|
|
|
export class Authentication {
|
|
|
|
public tokens: Tokens
|
|
|
|
public accessTokenAge: number
|
2022-08-24 17:22:55 +02:00
|
|
|
public socket?: Socket
|
2021-10-24 06:09:43 +02:00
|
|
|
public api: AxiosInstance
|
|
|
|
|
2022-08-24 17:22:55 +02:00
|
|
|
constructor(tokens: Tokens, disableSocketIO: boolean = false) {
|
2021-10-24 06:09:43 +02:00
|
|
|
this.tokens = tokens
|
|
|
|
this.accessTokenAge = Date.now()
|
2022-08-24 17:22:55 +02:00
|
|
|
if (disableSocketIO || typeof window === 'undefined') {
|
|
|
|
this.socket = undefined
|
|
|
|
} else {
|
|
|
|
this.socket = io(API_URL, {
|
|
|
|
auth: { token: `Bearer ${tokens.accessToken}` }
|
|
|
|
})
|
|
|
|
this.socket.on('connect', () => {
|
|
|
|
console.log(
|
|
|
|
`Connected to socket with clientId: ${this.socket?.id ?? 'undefined'}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-10-24 06:09:43 +02:00
|
|
|
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)
|
|
|
|
}
|
2021-10-26 16:38:55 +02:00
|
|
|
config.headers = config.headers == null ? {} : config.headers
|
2021-10-24 06:09:43 +02:00
|
|
|
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}`
|
2022-08-24 17:22:55 +02:00
|
|
|
if (typeof this?.socket?.auth !== 'function' && this.socket != null) {
|
2021-10-24 06:09:43 +02:00
|
|
|
this.socket.auth.token = token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public signout(): void {
|
|
|
|
cookies.remove('refreshToken')
|
2022-08-24 17:22:55 +02:00
|
|
|
window.localStorage.clear()
|
2021-10-24 06:09:43 +02:00
|
|
|
window.location.href = '/authentication/signin'
|
|
|
|
}
|
|
|
|
|
|
|
|
public async signoutServerSide(): Promise<void> {
|
2022-08-29 19:58:50 +02:00
|
|
|
try {
|
|
|
|
await this.api.post('/users/signout', {
|
|
|
|
refreshToken: this.tokens.refreshToken
|
|
|
|
})
|
|
|
|
} catch {}
|
2021-10-24 06:09:43 +02:00
|
|
|
this.signout()
|
|
|
|
}
|
|
|
|
|
2022-08-29 20:15:47 +02:00
|
|
|
public async signoutAllDevicesServerSide(): Promise<void> {
|
|
|
|
try {
|
|
|
|
await this.api.delete('/users/signout')
|
|
|
|
} catch {}
|
|
|
|
this.signout()
|
|
|
|
}
|
|
|
|
|
2021-10-24 06:09:43 +02:00
|
|
|
public signin(): void {
|
|
|
|
cookies.set('refreshToken', this.tokens.refreshToken)
|
|
|
|
}
|
|
|
|
}
|