22 * This file contains routes and sample routes for possible APIs usecases with Kinde.
33 */
44
5- import type { UserProfileType } from '@local/common/src/types/user'
65// import type { ClaimTokenType, FlagType } from '@kinde-oss/kinde-typescript-sdk'
76import { appFactory } from '#src/helpers/factory.js'
87import { getSessionManager } from '#src/helpers/kinde.js'
98import { getKindeClient } from '#src/providers/auth/kinde-main.js'
9+ import { objectOmit } from '@local/common/src/utils/general'
1010import { env } from 'std-env'
1111
1212export const authRoutesApp = appFactory . createApp ( )
1313 . get ( '/health' , async ( c ) => {
1414 return c . text ( 'Good' , 200 )
1515 } )
1616
17+ // This endpoint returns the current auth state
1718 . get ( '/authState' , async ( c ) => {
18- const kindeClient = await getKindeClient ( )
19- const sessionManager = getSessionManager ( c )
19+ const session = c . get ( 'session' )
2020
21- const [ profile , token ] = await Promise . all ( [
22- kindeClient . getUserProfile ( sessionManager ) . catch ( ( ) => null ) as Promise < UserProfileType > ,
23- kindeClient . getToken ( sessionManager ) . catch ( ( ) => null ) ,
24- ] )
21+ const userAuth = session . data . userAuth
22+ const tokens = userAuth ?. tokens ?? null
2523
26- return c . json ( { profile , token } )
24+ return c . json ( { userAuth : userAuth ? objectOmit ( userAuth , [ 'tokens' ] ) : null , tokens } )
2725 } )
2826
2927 . get ( '/login' , async ( c ) => {
3028 const kindeClient = await getKindeClient ( )
3129 const org_code = c . req . query ( 'org_code' )
30+ const session = c . get ( 'session' )
3231
3332 const loginUrl = await kindeClient . login ( getSessionManager ( c ) , { org_code } )
3433
35- c . get ( ' session' ) . set ( ' backToPath' , c . req . query ( 'path' ) )
34+ session . data . backToPath = c . req . query ( 'path' )
3635
3736 return c . redirect ( loginUrl . toString ( ) )
3837 } )
@@ -48,13 +47,30 @@ export const authRoutesApp = appFactory.createApp()
4847
4948 . get ( '/callback' , async ( c ) => {
5049 const kindeClient = await getKindeClient ( )
50+ const session = c . get ( 'session' )
51+ const sessionManager = getSessionManager ( c )
5152
52- await kindeClient . handleRedirectToApp ( getSessionManager ( c ) , new URL ( c . req . url ) )
53+ await kindeClient . handleRedirectToApp ( sessionManager , new URL ( c . req . url ) )
5354
54- let backToPath = c . get ( ' session' ) . get ( ' backToPath' ) as string || '/'
55+ let backToPath = session . data . backToPath as string || '/'
5556 if ( ! backToPath . startsWith ( '/' ) )
5657 backToPath = `/${ backToPath } `
5758
59+ const kindeProfile = await kindeClient . getUserProfile ( sessionManager )
60+
61+ session . data . userAuth = {
62+ id : kindeProfile . id ,
63+ avatar : kindeProfile . picture || undefined ,
64+ email : kindeProfile . email ,
65+ firstName : kindeProfile . given_name ,
66+ // @ts -expect-error Kinde SDK is dumb
67+ fullName : kindeProfile . name ,
68+
69+ tokens : {
70+ accessToken : await kindeClient . getToken ( sessionManager ) ,
71+ } ,
72+ }
73+
5874 return c . redirect ( `${ env . FRONTEND_URL ! } ${ backToPath } ` )
5975 } )
6076
@@ -66,13 +82,14 @@ export const authRoutesApp = appFactory.createApp()
6682 return c . redirect ( logoutUrl . toString ( ) )
6783 } )
6884
69- // .get('/isAuth', async (c) => {
70- // const kindeClient = await getKindeClient()
85+ // This endpoint checks if kinde session is authenticated
86+ . get ( '/isAuth' , async ( c ) => {
87+ const kindeClient = await getKindeClient ( )
7188
72- // const isAuthenticated = await kindeClient.isAuthenticated(getSessionManager(c)) // Boolean: true or false
89+ const isAuthenticated = await kindeClient . isAuthenticated ( getSessionManager ( c ) )
7390
74- // return c.json(isAuthenticated)
75- // })
91+ return c . json ( isAuthenticated )
92+ } )
7693
7794// .get('/profile', async (c) => {
7895// const kindeClient = await getKindeClient()
0 commit comments