Skip to content

Commit

Permalink
feat: get user info as part of the authenticate endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropapa committed Oct 7, 2023
1 parent 3a10409 commit 58f9522
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
55 changes: 47 additions & 8 deletions src/pages/authenticate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type APIRoute } from "astro";
import type { OAuthToken } from "../types/oauth.types";
import type { OAuthToken, OAuthUserInfo } from "../types/oauth.types";

export const POST: APIRoute = async ({ request }) => {
const { code } = await request.json();
Expand All @@ -15,14 +15,55 @@ export const POST: APIRoute = async ({ request }) => {
);
}

try {
const token = await getTokenInfo(code);
const user = await getUserInfo(token.access_token);

return new Response(
JSON.stringify({
token,
user,
})
);
} catch (error) {
console.error(error);
return new Response(
JSON.stringify({
error,
}),
{
status: 400,
}
);
}
};

const getUserInfo = async (accessToken: string): Promise<OAuthUserInfo> => {
const request = await fetch(
`https://${import.meta.env.PUBLIC_AUTH0_DOMAIN}/userinfo`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);

if (request.status !== 200) {
throw new Error("Error fetching auth token");
}

return await request.json();
};

const getTokenInfo = async (code: string): Promise<OAuthToken> => {
const formData = new URLSearchParams();
formData.append("grant_type", "authorization_code");
formData.append("client_id", import.meta.env.PUBLIC_AUTH0_CLIENT_ID);
formData.append("client_secret", import.meta.env.AUTH0_SECRET);
formData.append("code", code);
formData.append("redirect_uri", import.meta.env.PUBLIC_AUTH0_REDIRECT_URI);

const authTokenRequest = await fetch(
const request = await fetch(
`https://${import.meta.env.PUBLIC_AUTH0_DOMAIN}/oauth/token`,
{
method: "POST",
Expand All @@ -33,11 +74,9 @@ export const POST: APIRoute = async ({ request }) => {
}
);

const authToken = (await authTokenRequest.json()) as OAuthToken;

const response = new Response(JSON.stringify(authToken));

response.headers.set("Content-Type", "application/json");
if (request.status !== 200) {
throw new Error("Error fetching auth token");
}

return response;
return await request.json();
};
27 changes: 26 additions & 1 deletion src/types/oauth.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,30 @@ export type OAuthToken = {
id_token: string;
scope: string;
expires_in: number;
token_type: "Bearer";
token_type: string;
};

export type OAuthUserInfo = {
sub: string;
name: string;
given_name: string;
family_name: string;
middle_name: string;
nickname: string;
preferred_username: string;
profile: string;
picture: string;
website: string;
email: string;
email_verified: boolean;
gender: string;
birthdate: string;
zoneinfo: string;
locale: string;
phone_number: string;
phone_number_verified: boolean;
address: {
country: string;
};
updated_at: string;
};

0 comments on commit 58f9522

Please sign in to comment.