2024-02-28 16:49:44 +00:00
|
|
|
import { EventTemplate } from 'nostr-tools'
|
|
|
|
import { MetadataController, NostrController } from '.'
|
|
|
|
import { setAuthState, setMetadataEvent } from '../store/actions'
|
|
|
|
import store from '../store/store'
|
2024-03-19 10:27:18 +00:00
|
|
|
import {
|
|
|
|
base64DecodeAuthToken,
|
|
|
|
base64EncodeSignedEvent,
|
|
|
|
getAuthToken,
|
|
|
|
getVisitedLink,
|
|
|
|
saveAuthToken
|
|
|
|
} from '../utils'
|
2024-02-28 16:49:44 +00:00
|
|
|
import { appPrivateRoutes } from '../routes'
|
2024-03-20 04:44:10 +00:00
|
|
|
import { SignedEvent } from '../types'
|
2024-02-28 16:49:44 +00:00
|
|
|
|
|
|
|
export class AuthController {
|
|
|
|
private nostrController: NostrController
|
|
|
|
private metadataController: MetadataController
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.nostrController = NostrController.getInstance()
|
|
|
|
this.metadataController = new MetadataController()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function will authenticate user by signing an auth event
|
|
|
|
* which is done by calling the sign() function, where appropriate
|
|
|
|
* method will be chosen (extension, nsecbunker or keys)
|
|
|
|
*
|
|
|
|
* @param pubkey of the user trying to login
|
|
|
|
* @returns url to redirect if authentication successfull
|
|
|
|
* or error if otherwise
|
|
|
|
*/
|
|
|
|
async authenticateAndFindMetadata(pubkey: string) {
|
|
|
|
this.metadataController
|
|
|
|
.findMetadata(pubkey)
|
|
|
|
.then((event) => {
|
|
|
|
store.dispatch(setMetadataEvent(event))
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error('Error occurred while finding metadata', err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Nostr uses unix timestamps
|
|
|
|
const timestamp = Math.floor(Date.now() / 1000)
|
|
|
|
const { hostname } = window.location
|
|
|
|
|
|
|
|
const authEvent: EventTemplate = {
|
|
|
|
kind: 1,
|
|
|
|
tags: [],
|
|
|
|
content: `${hostname}-${timestamp}`,
|
|
|
|
created_at: timestamp
|
|
|
|
}
|
|
|
|
|
2024-03-19 10:27:18 +00:00
|
|
|
const signedAuthEvent = await this.nostrController.signEvent(authEvent)
|
|
|
|
this.createAndSaveAuthToken(signedAuthEvent)
|
2024-02-28 16:49:44 +00:00
|
|
|
|
|
|
|
store.dispatch(
|
|
|
|
setAuthState({
|
2024-03-01 10:16:35 +00:00
|
|
|
loggedIn: true,
|
|
|
|
usersPubkey: pubkey
|
2024-02-28 16:49:44 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const visitedLink = getVisitedLink()
|
|
|
|
|
|
|
|
if (visitedLink) {
|
|
|
|
const { pathname, search } = visitedLink
|
|
|
|
|
|
|
|
return Promise.resolve(`${pathname}${search}`)
|
|
|
|
} else {
|
|
|
|
// Navigate user in
|
|
|
|
return Promise.resolve(appPrivateRoutes.homePage)
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 10:27:18 +00:00
|
|
|
|
|
|
|
checkSession() {
|
|
|
|
const savedAuthToken = getAuthToken()
|
|
|
|
|
2024-03-20 04:44:10 +00:00
|
|
|
if (savedAuthToken) {
|
|
|
|
const signedEvent = base64DecodeAuthToken(savedAuthToken)
|
2024-03-19 10:27:18 +00:00
|
|
|
|
|
|
|
store.dispatch(
|
|
|
|
setAuthState({
|
|
|
|
loggedIn: true,
|
|
|
|
usersPubkey: signedEvent.pubkey
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
store.dispatch(
|
|
|
|
setAuthState({
|
|
|
|
loggedIn: false,
|
|
|
|
usersPubkey: undefined
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private createAndSaveAuthToken(signedAuthEvent: SignedEvent) {
|
|
|
|
const base64Encoded = base64EncodeSignedEvent(signedAuthEvent)
|
|
|
|
|
|
|
|
// save newly created auth token (base64 nostr singed event) in local storage along with expiry time
|
2024-03-20 04:44:10 +00:00
|
|
|
saveAuthToken(base64Encoded)
|
2024-03-19 10:27:18 +00:00
|
|
|
return base64Encoded
|
|
|
|
}
|
2024-02-28 16:49:44 +00:00
|
|
|
}
|