authorizer 2.0 code adaptation
This commit is contained in:
parent
0b443804bd
commit
103e73a870
|
@ -10,6 +10,8 @@ import {
|
||||||
ConfigType,
|
ConfigType,
|
||||||
SignupInput,
|
SignupInput,
|
||||||
AuthorizeResponse,
|
AuthorizeResponse,
|
||||||
|
ApiResponse,
|
||||||
|
GenericResponse,
|
||||||
// GraphqlQueryInput,
|
// GraphqlQueryInput,
|
||||||
} from '@authorizerdev/authorizer-js'
|
} from '@authorizerdev/authorizer-js'
|
||||||
import {
|
import {
|
||||||
|
@ -121,21 +123,31 @@ export const SessionProvider = (props: {
|
||||||
// Function to load session data
|
// Function to load session data
|
||||||
const sessionData = async () => {
|
const sessionData = async () => {
|
||||||
try {
|
try {
|
||||||
const s = await authorizer().getSession()
|
const s: ApiResponse<AuthToken> = await authorizer().getSession()
|
||||||
|
if (s?.data) {
|
||||||
console.info('[context.session] loading session', s)
|
console.info('[context.session] loading session', s)
|
||||||
|
|
||||||
// Set session expiration time in local storage
|
// Set session expiration time in local storage
|
||||||
const expires_at = new Date(Date.now() + s.expires_in * 1000)
|
const expires_at = new Date(Date.now() + s.data.expires_in * 1000)
|
||||||
localStorage.setItem('expires_at', `${expires_at.getTime()}`)
|
localStorage.setItem('expires_at', `${expires_at.getTime()}`)
|
||||||
|
|
||||||
// Set up session expiration check timer
|
// Set up session expiration check timer
|
||||||
minuteLater = setTimeout(checkSessionIsExpired, 60 * 1000)
|
minuteLater = setTimeout(checkSessionIsExpired, 60 * 1000)
|
||||||
console.info(`[context.session] will refresh in ${s.expires_in / 60} mins`)
|
console.info(`[context.session] will refresh in ${s.data.expires_in / 60} mins`)
|
||||||
|
|
||||||
// Set the session loaded flag
|
// Set the session loaded flag
|
||||||
setIsSessionLoaded(true)
|
setIsSessionLoaded(true)
|
||||||
|
|
||||||
return s
|
return s.data
|
||||||
|
} else {
|
||||||
|
console.info('[context.session] cannot refresh session', s.errors)
|
||||||
|
setAuthError(s.errors.pop().message)
|
||||||
|
|
||||||
|
// Set the session loaded flag even if there's an error
|
||||||
|
setIsSessionLoaded(true)
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.info('[context.session] cannot refresh session', error)
|
console.info('[context.session] cannot refresh session', error)
|
||||||
setAuthError(error)
|
setAuthError(error)
|
||||||
|
@ -258,17 +270,20 @@ export const SessionProvider = (props: {
|
||||||
|
|
||||||
// authorizer api proxy methods
|
// authorizer api proxy methods
|
||||||
const signUp = async (params: SignupInput) => {
|
const signUp = async (params: SignupInput) => {
|
||||||
const authResult: void | AuthToken = await authorizer().signup(params)
|
const authResult: ApiResponse<AuthToken> = await authorizer().signup(params)
|
||||||
if (authResult) setSession(authResult)
|
if (authResult?.data) setSession(authResult.data)
|
||||||
|
if (authResult?.errors) console.error(authResult.errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
const signIn = async (params: LoginInput) => {
|
const signIn = async (params: LoginInput) => {
|
||||||
const authResult: AuthToken | void = await authorizer().login(params)
|
const authResult: ApiResponse<AuthToken> = await authorizer().login(params)
|
||||||
if (authResult) setSession(authResult)
|
if (authResult?.data) setSession(authResult.data)
|
||||||
|
if (authResult?.errors) console.error(authResult.errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
const signOut = async () => {
|
const signOut = async () => {
|
||||||
await authorizer().logout()
|
const authResult: ApiResponse<GenericResponse> = await authorizer().logout()
|
||||||
|
console.debug(authResult)
|
||||||
reset()
|
reset()
|
||||||
showSnackbar({ body: t("You've successfully logged out") })
|
showSnackbar({ body: t("You've successfully logged out") })
|
||||||
}
|
}
|
||||||
|
@ -281,9 +296,13 @@ export const SessionProvider = (props: {
|
||||||
const confirmEmail = async (input: VerifyEmailInput) => {
|
const confirmEmail = async (input: VerifyEmailInput) => {
|
||||||
console.debug(`[context.session] calling authorizer's verify email with`, input)
|
console.debug(`[context.session] calling authorizer's verify email with`, input)
|
||||||
try {
|
try {
|
||||||
const at: void | AuthToken = await authorizer().verifyEmail(input)
|
const at: ApiResponse<AuthToken> = await authorizer().verifyEmail(input)
|
||||||
if (at) setSession(at)
|
if (at?.data) {
|
||||||
return at
|
setSession(at.data)
|
||||||
|
return at.data
|
||||||
|
} else {
|
||||||
|
console.warn(at?.errors)
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(error)
|
console.warn(error)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user