-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #824 from visualize-admin/auth
- Loading branch information
Showing
27 changed files
with
988 additions
and
1,822 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 |
---|---|---|
@@ -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.
b32e5aa
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.
Successfully deployed to the following URLs:
visualization-tool – ./
visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app
visualization-tool-alpha.vercel.app