-
Notifications
You must be signed in to change notification settings - Fork 3
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
Authentication scaffold #824
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
880c53a
refactor: Deprecation
ptbrowne 7a87e2e
refactor: Remove unused
ptbrowne 3dd5d26
fix: Use syntax recognized by both yarn and npm
ptbrowne ed5454d
fix: Allow undefined value for prop in no-large-sx rule
ptbrowne cf25b0d
docs: Add keycloak documentation
ptbrowne 604548b
feat: Add scaffold for auth
ptbrowne 39d0726
feat: Use Prisma
ptbrowne 7e616cf
feat: Ensures authenticated user exists on our side
ptbrowne 67ec123
feat: Add login menu (not activated if Keycloak not configured)
ptbrowne 703e0bb
feat: Attach created chart to user
ptbrowne 898cce0
feat: Add profile page listing user charts
ptbrowne 40d7ce6
feat: Bypass database migration if DATABASE_URL not set
ptbrowne cda44e4
chore: Review comments
ptbrowne 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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Typography, Button, Box } from "@mui/material"; | ||
import { getProviders, signIn, signOut, useSession } from "next-auth/react"; | ||
import Link from "next/link"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { Awaited } from "@/domain/types"; | ||
|
||
type Providers = Awaited<ReturnType<typeof getProviders>>; | ||
|
||
const useProviders = () => { | ||
const [state, setState] = useState({ | ||
status: "loading", | ||
data: undefined as Providers | undefined, | ||
}); | ||
useEffect(() => { | ||
const run = async () => { | ||
const providers = await getProviders(); | ||
setState({ status: "loaded", data: providers }); | ||
}; | ||
run(); | ||
}, []); | ||
return state; | ||
}; | ||
|
||
function LoginMenu() { | ||
const { data: session, status: sessionStatus } = useSession(); | ||
const { data: providers, status: providersStatus } = useProviders(); | ||
if (sessionStatus === "loading" || providersStatus === "loading") { | ||
return null; | ||
} | ||
if (!providers || !Object.keys(providers).length) { | ||
return null; | ||
} | ||
return ( | ||
<Box sx={{ alignItems: "center", display: "flex" }}> | ||
{session ? ( | ||
<> | ||
<Typography variant="body2"> | ||
Signed in as <Link href="/profile">{session.user?.name}</Link>{" "} | ||
{session.user?.id} | ||
{" - "} | ||
</Typography> | ||
<Button | ||
variant="text" | ||
color="primary" | ||
size="small" | ||
onClick={async () => await signOut()} | ||
> | ||
Sign out | ||
</Button> | ||
</> | ||
) : ( | ||
<> | ||
<Typography variant="body2"> | ||
Not signed in | ||
{" - "} | ||
</Typography> | ||
<Button | ||
variant="text" | ||
color="primary" | ||
size="small" | ||
onClick={() => signIn("keycloak")} | ||
> | ||
Sign in | ||
</Button> | ||
</> | ||
)} | ||
</Box> | ||
); | ||
} | ||
|
||
export default LoginMenu; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export default prisma; |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import SuperJSON from "superjson"; | ||
import { SuperJSONResult } from "superjson/dist/types"; | ||
|
||
export type Serialized<P> = P & { | ||
_superjson?: ReturnType<typeof SuperJSON.serialize>["meta"]; | ||
}; | ||
|
||
export const serializeProps = <T extends unknown>(props: T) => { | ||
const { json: sprops, meta } = SuperJSON.serialize(props); | ||
if (meta) { | ||
// @ts-ignore | ||
sprops._superjson = meta; | ||
} | ||
return sprops as Serialized<typeof props>; | ||
}; | ||
|
||
type Deserialized<T> = T extends Serialized<infer S> ? S : never; | ||
|
||
export const deserializeProps = <T extends Serialized<unknown>>(sprops: T) => { | ||
const { _superjson, ...props } = sprops; | ||
return SuperJSON.deserialize({ | ||
json: props, | ||
meta: _superjson, | ||
} as unknown as SuperJSONResult) as Deserialized<T>; | ||
}; |
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,49 @@ | ||
import prisma from "./client"; | ||
|
||
export const findBySub = async (sub: string) => { | ||
return prisma.user.findFirstOrThrow({ | ||
where: { | ||
sub, | ||
}, | ||
}); | ||
}; | ||
|
||
/** | ||
* Ensures an authenticated user has an account | ||
* on our side. | ||
* | ||
* - Uses the "sub" field from the JWT token to ensure | ||
* uniqueness. | ||
* - Updates the user name with what is found in the JWT token | ||
*/ | ||
export const ensureUserFromSub = async ( | ||
sub: string, | ||
name: string | undefined | null | ||
) => { | ||
const user = await prisma.user.findFirst({ | ||
where: { | ||
sub, | ||
}, | ||
}); | ||
if (user) { | ||
if (user.name !== name) { | ||
console.log(`Updating user name from auth provider info`); | ||
await prisma.user.update({ | ||
where: { | ||
id: user.id, | ||
}, | ||
data: { | ||
name: name, | ||
}, | ||
}); | ||
} | ||
return user; | ||
} else { | ||
const newUser = await prisma.user.create({ | ||
data: { | ||
sub: sub, | ||
}, | ||
}); | ||
return newUser; | ||
} | ||
}; |
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.
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.
TIL about SuperJSON 😍
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 did not know it before, it's part of a framework called Blitz. I think it would be interesting to check if it could be interesting for us.
I actually took most of the code for serialize from the babel plugin for superjson, that automatically adds superjson to your getServerSideProps : https://github.com/blitz-js/babel-plugin-superjson-next/blob/main/src/tools.tsx. I thought it was a little too magical (and based on babel) so I preferred to have it called explicitly.
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.
Looks interesting, especially the part about getting rid of GraphQL 😮