chore: no need to expire auth token
All checks were successful
Release / build_and_release (push) Successful in 43s

This commit is contained in:
Sabir Hassan 2024-03-20 09:44:10 +05:00
parent 2ed092bcbd
commit e57893b1b2
4 changed files with 7 additions and 41 deletions

View File

@ -10,7 +10,7 @@ import {
saveAuthToken
} from '../utils'
import { appPrivateRoutes } from '../routes'
import { AuthToken, SignedEvent } from '../types'
import { SignedEvent } from '../types'
export class AuthController {
private nostrController: NostrController
@ -76,8 +76,8 @@ export class AuthController {
checkSession() {
const savedAuthToken = getAuthToken()
if (savedAuthToken && this.isTokenValid(savedAuthToken)) {
const signedEvent = base64DecodeAuthToken(savedAuthToken.token)
if (savedAuthToken) {
const signedEvent = base64DecodeAuthToken(savedAuthToken)
store.dispatch(
setAuthState({
@ -97,25 +97,11 @@ export class AuthController {
)
}
private isTokenValid(authToken: AuthToken): boolean {
const timeNow = Math.round(Date.now() / 1000)
const eventExpiresAt = authToken.expiresAt
const timeDifference = eventExpiresAt - timeNow
// check if previous authToken has expired or not
if (timeDifference > 0) {
return true
}
return false
}
private createAndSaveAuthToken(signedAuthEvent: SignedEvent) {
const base64Encoded = base64EncodeSignedEvent(signedAuthEvent)
// save newly created auth token (base64 nostr singed event) in local storage along with expiry time
const createdAt = Math.round(Date.now() / 1000)
saveAuthToken(base64Encoded, createdAt + 3600) // 3600 secs = 1 hour
saveAuthToken(base64Encoded)
return base64Encoded
}
}

View File

@ -1,4 +0,0 @@
export interface AuthToken {
token: string
expiresAt: number
}

View File

@ -1,3 +1,2 @@
export * from './auth'
export * from './nostr'
export * from './profile'

View File

@ -1,5 +1,4 @@
import { State } from '../store/rootReducer'
import { AuthToken } from '../types'
export const saveState = (state: object) => {
try {
@ -55,26 +54,12 @@ export const getVisitedLink = () => {
}
}
export const saveAuthToken = (token: string, expiresAt: number) => {
localStorage.setItem(
'authToken',
JSON.stringify({
token,
expiresAt
})
)
export const saveAuthToken = (token: string) => {
localStorage.setItem('authToken', token)
}
export const getAuthToken = () => {
const serializedAuthDetail = localStorage.getItem('authToken')
if (!serializedAuthDetail) return null
try {
return JSON.parse(serializedAuthDetail) as AuthToken
} catch {
return null
}
return localStorage.getItem('authToken')
}
export const clearAuthToken = () => {