Skip to content
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

www: add auth hooks (#379) #399

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions www/lib/shuttle-api.ts

This file was deleted.

75 changes: 75 additions & 0 deletions www/lib/shuttle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import axios, {AxiosRequestConfig, AxiosResponse, HttpStatusCode, Method} from "axios";

export async function getApiKey(username: string): Promise<string> {
const res = await fetch(
`${process.env.SHUTTLE_API_BASE_URL}/users/${username}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.SHUTTLE_ADMIN_SECRET}`,
},
}
);

if (res.ok) {
const body = await res.json();
return body["key"]
} else {
console.log(res);
throw new Error("could not get api key.");
}
}

export type User = {
name: string
key: string
projects: string[]
}

export type Error = {
status: HttpStatusCode
error: string
}

export class Shuttle {
private url(suffix: string): string {
return `${process.env.SHUTTLE_API_BASE_URL}${suffix}`
}

private request(method: Method, path: string): Promise<Record<string, any>> {
let req = {
headers: {
Authorization: `Bearer ${process.env.SHUTTLE_ADMIN_SECRET}`
},
method: method,
url: this.url(path)
};
return axios.request(req).then((res) => {
return res.data;
}).catch((err) => {
if (err.response) {
return Promise.reject({
status: err.response.status,
...err.response.data
})
} else {
return Promise.reject(err);
}
})
}

async get_user(user: string): Promise<User> {
return this.request("GET", `/users/${user}`).then((body) => {
return body as User
})
}

async create_user(user: string): Promise<User> {
return this.request("POST", `/users/${user}`).then((body) => {
return body as User
})
}
}

export default new Shuttle();

1 change: 1 addition & 0 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@tailwindcss/forms": "^0.5.0",
"@tailwindcss/typography": "^0.5.2",
"@types/react-syntax-highlighter": "^13.5.2",
"axios": "^1.0.0",
"classnames": "^2.3.1",
"gray-matter": "^4.0.3",
"markdown-toc": "^1.2.0",
Expand Down
21 changes: 14 additions & 7 deletions www/pages/api/auth/[...auth0].ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { handleAuth, handleCallback, handleLogin } from "@auth0/nextjs-auth0";
import { getApiKey } from "../../../lib/shuttle-api";
import {handleAuth, handleCallback, handleLogin} from "@auth0/nextjs-auth0";
import shuttle, {Error} from "../../../lib/shuttle";

async function afterCallback(req, res, session, state) {
try {
session.user.api_key = await getApiKey(session.user.sub.replace("|", "-"));
} catch (err) {
console.error(err);
}
const shuttlified = session.user.sub.replace("|", "-");

const user = await shuttle.get_user(shuttlified).catch((err) => {
if ((err as Error).status === 404) {
console.log(`user ${shuttlified} does not exist, creating`);
return shuttle.create_user(shuttlified);
} else {
return Promise.reject(err);
}
});

session.user.api_key = user.key;

return session;
}
Expand Down
Loading