-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Arctic for OAuth providers #1851
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
132 changes: 132 additions & 0 deletions
132
waspc/data/Generator/templates/react-app/src/auth/pages/OAuthCallback.tsx
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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
{{={= =}=}} | ||
import { useEffect, useRef, useState } from "react"; | ||
import { type AxiosResponse } from "axios"; | ||
import { Redirect, useLocation } from 'react-router-dom' | ||
import { useAuth } from 'wasp/client/auth' | ||
import { api } from 'wasp/client/api' | ||
import { initSession } from 'wasp/auth/helpers/user' | ||
|
||
const wrapperStyles = { | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
padding: "4rem", | ||
}; | ||
|
||
const commonMessageStyles = { | ||
display: 'flex', | ||
alignItems: 'center', | ||
gap: '.5rem', | ||
borderRadius: '.5rem', | ||
padding: '1rem', | ||
}; | ||
|
||
const errorMessageStyles = { | ||
...commonMessageStyles, | ||
borderColor: 'rgb(240 82 82)', | ||
backgroundColor: 'rgb(253 232 232)', | ||
color: 'rgb(200 30 30)', | ||
}; | ||
|
||
const loadingMessageStyles = { | ||
...commonMessageStyles, | ||
borderColor: 'rgb(107 114 128)', | ||
backgroundColor: 'rgb(243 244 246)', | ||
color: 'rgb(55 65 81)', | ||
}; | ||
|
||
export function OAuthCallbackPage() { | ||
const { isLoading, error, user } = useOAuthCallbackHandler(); | ||
|
||
if (user !== undefined && user !== null) { | ||
return <Redirect to="{= onAuthSucceededRedirectTo =}" />; | ||
} | ||
|
||
return ( | ||
<div style={wrapperStyles}> | ||
{error && <div style={errorMessageStyles}><MessageIcon /> {error}</div>} | ||
{isLoading && <div style={loadingMessageStyles}><MessageIcon /> Please wait a moment while we log you in.</div>} | ||
</div> | ||
); | ||
} | ||
|
||
function useOAuthCallbackHandler() { | ||
const { data: user } = useAuth(); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [error, setError] = useState<string | null>(null); | ||
const location = useLocation(); | ||
|
||
async function handleCallback() { | ||
try { | ||
setIsLoading(true); | ||
const query = new URLSearchParams(location.search); | ||
|
||
// If we got redirect with an error, display it to the user | ||
// and don't continue with the login process. | ||
const errorFromRedirect = query.get('error'); | ||
if (errorFromRedirect !== null) { | ||
setError(errorFromRedirect); | ||
return; | ||
} | ||
|
||
const code = location.hash.slice(1); | ||
const response = await exchangeOAuthCodeForToken({ code }); | ||
if (!isResponseWithSessionId(response)) { | ||
setError("Unable to login with the OAuth provider."); | ||
return; | ||
} | ||
await initSession(response.data.sessionId); | ||
} catch (e: unknown) { | ||
console.error(e); | ||
setError("Unable to login with the OAuth provider."); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
} | ||
|
||
const isFirstRender = useRef(true); | ||
useEffect(() => { | ||
if (isFirstRender.current) { | ||
isFirstRender.current = false; | ||
handleCallback(); | ||
} | ||
}, []); | ||
sodic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
user, | ||
error, | ||
isLoading, | ||
}; | ||
} | ||
|
||
const MessageIcon = () => ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="1.25rem" | ||
height="1.25rem" | ||
fill="currentColor" | ||
stroke="currentColor" | ||
strokeWidth={0} | ||
aria-hidden="true" | ||
viewBox="0 0 20 20" | ||
> | ||
<path | ||
fillRule="evenodd" | ||
stroke="none" | ||
d="M18 10a8 8 0 1 1-16 0 8 8 0 0 1 16 0zm-7-4a1 1 0 1 1-2 0 1 1 0 0 1 2 0zM9 9a1 1 0 0 0 0 2v3a1 1 0 0 0 1 1h1a1 1 0 1 0 0-2v-3a1 1 0 0 0-1-1H9z" | ||
clipRule="evenodd" | ||
/> | ||
</svg> | ||
) | ||
|
||
async function exchangeOAuthCodeForToken(data: { | ||
code: string | ||
}): Promise<AxiosResponse<unknown>> { | ||
return api.post('/auth/exchange-code', data) | ||
} | ||
|
||
function isResponseWithSessionId( | ||
response: AxiosResponse<unknown> | ||
): response is AxiosResponse<{ sessionId: string }> { | ||
return response.data && typeof (response.data as any).sessionId === 'string' | ||
} |
70 changes: 0 additions & 70 deletions
70
waspc/data/Generator/templates/react-app/src/auth/pages/OAuthCodeExchange.jsx
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -16,19 +16,11 @@ export type ProviderConfig = { | |
// Unique provider identifier, used as part of URL paths | ||
id: ProviderName; | ||
displayName: string; | ||
// Each provider config can have an init method which is ran on setup time | ||
// e.g. for oAuth providers this is the time when the Passport strategy is registered. | ||
init?(provider: ProviderConfig): Promise<InitData>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed anymore |
||
// Every provider must have a setupRouter method which returns the Express router. | ||
// In this function we are flexibile to do what ever is necessary to make the provider work. | ||
createRouter(provider: ProviderConfig, initData: InitData): Router; | ||
createRouter(provider: ProviderConfig): Router; | ||
}; | ||
|
||
// PRIVATE API | ||
export type InitData = { | ||
[key: string]: any; | ||
} | ||
|
||
// PRIVATE API | ||
export type RequestWithWasp = Request & { wasp?: { [key: string]: any } } | ||
|
||
|
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
42 changes: 0 additions & 42 deletions
42
waspc/data/Generator/templates/server/src/auth/providers/config/_oauth.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come it can be both of these? I'm guessing because
undefined
happens on the first render, whilenull
happens when the user isn't logged in?If so, there's a trick that many linters recommend (although I'm not sure I like it). It sits halfway between being fully explicit and using coercion:
This covers both
undefined
andnull
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to skip the non strict check if possible. I know this looks ugly e.g. checking both
undefined
andnull
but it's at least not relying on implicit JS behaviour.