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 saveAuthToken
} from '../utils' } from '../utils'
import { appPrivateRoutes } from '../routes' import { appPrivateRoutes } from '../routes'
import { AuthToken, SignedEvent } from '../types' import { SignedEvent } from '../types'
export class AuthController { export class AuthController {
private nostrController: NostrController private nostrController: NostrController
@ -76,8 +76,8 @@ export class AuthController {
checkSession() { checkSession() {
const savedAuthToken = getAuthToken() const savedAuthToken = getAuthToken()
if (savedAuthToken && this.isTokenValid(savedAuthToken)) { if (savedAuthToken) {
const signedEvent = base64DecodeAuthToken(savedAuthToken.token) const signedEvent = base64DecodeAuthToken(savedAuthToken)
store.dispatch( store.dispatch(
setAuthState({ 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) { private createAndSaveAuthToken(signedAuthEvent: SignedEvent) {
const base64Encoded = base64EncodeSignedEvent(signedAuthEvent) const base64Encoded = base64EncodeSignedEvent(signedAuthEvent)
// save newly created auth token (base64 nostr singed event) in local storage along with expiry time // 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)
saveAuthToken(base64Encoded, createdAt + 3600) // 3600 secs = 1 hour
return 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 './nostr'
export * from './profile' export * from './profile'

View File

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