Skip to content

Commit

Permalink
Update OAuth examples to latest (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Aug 25, 2023
1 parent 68d156b commit 1a5a95f
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 29 deletions.
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/$astro.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,11 @@ export const get: APIRoute = async (context) => {
});
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -217,10 +218,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/$express.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ app.get("/login/github/callback", async (req, res) => {
return res.sendStatus(400);
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -212,10 +213,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/$hono.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ app.get("/login/github/callback", async (context) => {
return context.text("Bad request", 400);
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -189,10 +190,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/$nextjs-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ export const GET = async (request: NextRequest) => {
});
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -263,10 +264,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/$nextjs-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(400).end();
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -236,10 +237,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/$nuxt.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ export default defineEventHandler(async (event) => {
);
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -235,10 +236,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/$solidstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,11 @@ export const GET = async (event: APIEvent) => {
});
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -250,10 +251,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/$sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ export const GET = async ({ url, cookies, locals }) => {
});
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -236,10 +237,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/github-oauth/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ get("/login/github/callback", async (request: Request) => {
});
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down Expand Up @@ -226,10 +227,11 @@ You can check if the user has already registered with your app by checking `Gith
If they're a new user, you can create a new Lucia user (and key) with [`GithubUserAuth.createUser()`](/reference/oauth/interfaces#createuser). The type for `attributes` property is `Lucia.DatabaseUserAttributes`, which we added `github_username` to previously. You can access the Github user data with `GithubUserAuth.githubUser`, as well as the access tokens with `GithubUserAuth.githubTokens`.

```ts
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
6 changes: 4 additions & 2 deletions documentation/content/guidebook/oauth-account-linking.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ When providing more than one ways to sign in, you may want to link multiple prov
Here's a basic OAuth implementation using the official integration.

```ts
const { existingUser, createUser, providerUser } =
const { getExistingUser, createUser, providerUser } =
providerAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
if (!providerUser.email_verified) {
throw new Error("Email not verified");
Expand All @@ -39,10 +40,11 @@ It's important to note `existingUser` is defined if a user linked to the provide
```ts
import { auth } from "./lucia.js";

const { existingUser, createUser, providerUser, createKey } =
const { getExistingUser, createUser, providerUser, createKey } =
providerAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
if (!providerUser.email_verified) {
throw new Error("Email not verified");
Expand Down
2 changes: 1 addition & 1 deletion documentation/content/oauth/providers/discord.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type DiscordUser = {
premium_type?: number;
public_flags?: number;
locale?: string;
avatar_decoration? :string;
avatar_decoration?: string;
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ export const get: APIRoute = async (context) => {
});
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
3 changes: 2 additions & 1 deletion examples/express/github-oauth/src/routes/login/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ router.get("/login/github/callback", async (req, res) => {
return res.sendStatus(400);
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ export const GET = async (request: NextRequest) => {
});
}
try {
const { existingUser, appleUser, createUser } =
const { getExistingUser, appleUser, createUser } =
await appleAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ export const GET = async (request: NextRequest) => {
});
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(400).end();
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export default defineEventHandler(async (event) => {
);
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ export const GET = async (event: APIEvent) => {
});
}
try {
const { existingUser, githubUser, createUser } =
const { getExistingUser, githubUser, createUser } =
await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export const GET = async ({ url, cookies, locals }) => {
});
}
try {
const { existingUser, githubUser, createUser } = await githubAuth.validateCallback(code);
const { getExistingUser, githubUser, createUser } = await githubAuth.validateCallback(code);

const getUser = async () => {
const existingUser = await getExistingUser();
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
Expand Down

0 comments on commit 1a5a95f

Please sign in to comment.