Skip to content

Commit

Permalink
feat: http shorthand auth example
Browse files Browse the repository at this point in the history
  • Loading branch information
sejori committed Apr 27, 2023
1 parent 694123a commit 0201aee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
14 changes: 14 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 29 additions & 31 deletions examples/auth/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,49 @@ import * as Peko from "https://deno.land/x/peko/mod.ts"

const server = new Peko.Server()
const crypto = new Peko.Crypto("SUPER_SECRET_KEY_123") // <-- replace from env
const user = {
const user = { // <-- replace with db / auth provider query
username: "test-user",
password: await crypto.hash("test-password")
} // <-- replace with db / auth provider query
}

const validateUser = async (username: string, password: string) => {
return username && password
&& username === user.username
&& await crypto.hash(password) === user.password
&& username === user.username
&& await crypto.hash(password) === user.password
}

server.use(Peko.logger(console.log))
server.addRoute("/login", {
method: "POST",
handler: async (ctx) => {
const { username, password } = await ctx.request.json()

if (!await validateUser(username, password)) {
return new Response("Bad credentials", {status: 401 })
}

const exp = new Date()
exp.setMonth(exp.getMonth() + 1)
const jwt = await crypto.sign({
iat: Date.now(),
exp: exp.valueOf(),
data: { user: user.username }
})
server.post("/login", async (ctx) => {
const { username, password } = await ctx.request.json()

return new Response(jwt, {
status: 201,
headers: new Headers({
"Content-Type": "application/json"
})
})
if (!await validateUser(username, password)) {
return new Response("Bad credentials", {status: 401 })
}
})

server.addRoute("/verify", {
middleware: Peko.authenticator(crypto),
handler: () => new Response("You are authenticated!")
const exp = new Date()
exp.setMonth(exp.getMonth() + 1)
const jwt = await crypto.sign({
iat: Date.now(),
exp: exp.valueOf(),
data: { user: user.username }
})

return new Response(jwt, {
status: 201,
headers: new Headers({
"Content-Type": "application/json"
})
})
})

server.get(
"/verify",
Peko.authenticator(crypto),
() => new Response("You are authenticated!")
)

const html = String
server.addRoute("/", Peko.ssrHandler(() => html`<!doctype html>
server.get("/", Peko.ssrHandler(() => html`<!doctype html>
<html lang="en">
<head>
<title>Peko auth example</title>
Expand Down

0 comments on commit 0201aee

Please sign in to comment.