-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15139b6
commit f5a8bdf
Showing
272 changed files
with
3,432 additions
and
17,826 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,74 @@ | ||
import type { AuthOptions, AuthProviderProps } from '@saas-ui/auth-provider' | ||
|
||
import type { | ||
Auth0Client, | ||
RedirectLoginOptions, | ||
User, | ||
} from '@auth0/auth0-spa-js' | ||
import type { AuthOptions, AuthProviderProps } from '@saas-ui/auth-provider' | ||
|
||
export const createAuthService = <Client extends Auth0Client>( | ||
auth0Client: Client | ||
auth0Client: Client, | ||
): AuthProviderProps<User> => { | ||
const onRestoreAuthState = async () => { | ||
if ( | ||
typeof window !== 'undefined' && | ||
window.location?.search?.includes('code=') && | ||
window.location?.search?.includes('state=') | ||
) { | ||
const { appState } = await auth0Client.handleRedirectCallback( | ||
window.location.href | ||
) | ||
return { | ||
onRestoreAuthState: async () => { | ||
if ( | ||
typeof window !== 'undefined' && | ||
window.location?.search?.includes('code=') && | ||
window.location?.search?.includes('state=') | ||
) { | ||
const { appState } = await auth0Client.handleRedirectCallback( | ||
window.location.href, | ||
) | ||
|
||
if (appState && appState.targetUrl) { | ||
window.location?.assign(appState.targetUrl) | ||
} else { | ||
history.replaceState({}, document.title, window.location.pathname) | ||
if (appState && appState.targetUrl) { | ||
window.location?.assign(appState.targetUrl) | ||
} else { | ||
history.replaceState({}, document.title, window.location.pathname) | ||
} | ||
} | ||
} | ||
} | ||
|
||
const onLogin = async ( | ||
params?: object, | ||
options: AuthOptions<RedirectLoginOptions> = {} | ||
) => { | ||
const { redirectTo, ...loginOptions } = options | ||
|
||
await auth0Client.loginWithRedirect({ | ||
...loginOptions, | ||
authorizationParams: { | ||
redirect_uri: redirectTo ?? window.location.href, | ||
}, | ||
}) | ||
return null | ||
} | ||
}, | ||
onLogin: async ( | ||
_params?: object, | ||
options: AuthOptions<RedirectLoginOptions> = {}, | ||
) => { | ||
const { redirectTo, ...loginOptions } = options | ||
|
||
const onSignup = async ( | ||
params?: object, | ||
options: AuthOptions<RedirectLoginOptions> = {} | ||
) => { | ||
const { redirectTo, ...loginOptions } = options | ||
await auth0Client.loginWithRedirect({ | ||
...loginOptions, | ||
authorizationParams: { | ||
redirect_uri: redirectTo ?? window.location.href, | ||
}, | ||
}) | ||
return null | ||
}, | ||
onSignup: async ( | ||
_params?: object, | ||
options: AuthOptions<RedirectLoginOptions> = {}, | ||
) => { | ||
const { redirectTo, ...loginOptions } = options | ||
|
||
await auth0Client.loginWithRedirect({ | ||
...loginOptions, | ||
authorizationParams: { | ||
screen_hint: 'signup', | ||
prompt: 'login', | ||
redirect_uri: options?.redirectTo ?? window.location.href, | ||
}, | ||
}) | ||
return null | ||
} | ||
|
||
const onLogout = async () => { | ||
return await auth0Client.logout({ | ||
logoutParams: { returnTo: window.location.origin }, | ||
}) | ||
} | ||
|
||
const onLoadUser = async () => { | ||
const user = await auth0Client.getUser() | ||
return user || null | ||
} | ||
|
||
const onGetToken = async () => { | ||
return auth0Client.getTokenSilently({ | ||
timeoutInSeconds: 2, | ||
}) | ||
} | ||
|
||
return { | ||
onRestoreAuthState, | ||
onLogin, | ||
onSignup, | ||
onLogout, | ||
onLoadUser, | ||
onGetToken, | ||
} | ||
await auth0Client.loginWithRedirect({ | ||
...loginOptions, | ||
authorizationParams: { | ||
screen_hint: 'signup', | ||
prompt: 'login', | ||
redirect_uri: options?.redirectTo ?? window.location.href, | ||
}, | ||
}) | ||
return null | ||
}, | ||
onLogout: async () => { | ||
return await auth0Client.logout({ | ||
logoutParams: { returnTo: window.location.origin }, | ||
}) | ||
}, | ||
onLoadUser: async () => { | ||
const user = await auth0Client.getUser() | ||
return user || null | ||
}, | ||
onGetToken: async () => { | ||
return auth0Client.getTokenSilently({ | ||
timeoutInSeconds: 2, | ||
}) | ||
}, | ||
} satisfies AuthProviderProps<User> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.