-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
531 additions
and
314 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
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
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 |
---|---|---|
@@ -1,22 +1,38 @@ | ||
import { Elysia, t } from "elysia"; | ||
import { authUser } from "./auth"; | ||
import { getUser } from "../db/repo-user"; | ||
import { user } from "./auth"; | ||
import { getUser, updateUser } from "../db/repo-user"; | ||
import { UserSchema } from "../types"; | ||
|
||
export const profile = new Elysia().use(authUser).get( | ||
"/profile", | ||
async ({ user }) => { | ||
if (user.type !== "user") { | ||
throw new Error(`Not a user token , received "${user.type}"`); | ||
} | ||
return await getUser(user.id); | ||
}, | ||
{ | ||
detail: { | ||
summary: "Get your profile", | ||
export const profile = new Elysia() | ||
.use(user) | ||
.get( | ||
"/profile", | ||
async ({ user }) => { | ||
if (user.type !== "user") { | ||
throw new Error(`Not a user token , received "${user.type}"`); | ||
} | ||
return await getUser(user.id); | ||
}, | ||
response: { | ||
200: t.Ref(UserSchema), | ||
{ | ||
detail: { | ||
summary: "Get your profile", | ||
}, | ||
response: { | ||
200: t.Ref(UserSchema), | ||
}, | ||
}, | ||
}, | ||
); | ||
) | ||
.put( | ||
"/profile", | ||
async ({ user, body }) => { | ||
if (user.type !== "user") { | ||
throw new Error(`Not a user token , received "${user.type}"`); | ||
} | ||
await updateUser(user.id, body); | ||
}, | ||
{ | ||
body: t.Object({ | ||
settingAutoRefetch: t.Optional(t.Boolean()), | ||
}), | ||
}, | ||
); |
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
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
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,51 @@ | ||
import { Elysia, t } from "elysia"; | ||
import { jwt } from "@elysiajs/jwt"; | ||
import bearer from "@elysiajs/bearer"; | ||
import type { Static } from "elysia"; | ||
|
||
const userSchema = t.Union([ | ||
// User tokens describe a user interacting with the API. | ||
t.Object({ | ||
type: t.Literal("user"), | ||
id: t.Number(), | ||
}), | ||
// Service tokens, such as Stitcher. | ||
t.Object({ type: t.Literal("service") }), | ||
]); | ||
|
||
export type User = Static<typeof userSchema>; | ||
|
||
export function bearerAuth(secret: string) { | ||
const jwtUser = jwt({ | ||
name: "jwtUser", | ||
schema: userSchema, | ||
secret, | ||
}); | ||
|
||
const user = new Elysia() | ||
.use(bearer()) | ||
.use(jwtUser) | ||
.derive({ as: "scoped" }, async ({ bearer, jwtUser, set }) => { | ||
const token = await jwtUser.verify(bearer); | ||
if (!token) { | ||
set.status = 401; | ||
throw new Error("Unauthorized"); | ||
} | ||
if (token.type === "user") { | ||
return { | ||
user: { type: "user", id: token.id }, | ||
}; | ||
} | ||
if (token.type === "service") { | ||
return { | ||
user: { type: "service" }, | ||
}; | ||
} | ||
throw new Error("Invalid token type"); | ||
}); | ||
|
||
return { | ||
jwtUser, | ||
user, | ||
}; | ||
} |
Oops, something went wrong.