Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
/* routes.js */
import { Router } from 'oak'
import HandlebarsEnvironment from 'handlebars'
import { login, register } from 'accounts'
const router = new Router()
// the routes defined here
router.get('/', async context => {
const authorised = await context.cookies.get('authorised')
const data = { authorised }
const templateString = await Deno.readTextFile("./views/home.hbs")
const template = HandlebarsEnvironment.compile(templateString)
const body = template(data)
context.response.body = body
})
router.get('/login', async context => {
const templateString = await Deno.readTextFile("./views/login.hbs")
const template = HandlebarsEnvironment.compile(templateString)
const body = template()
context.response.body = body
})
router.get('/register', async context => {
const templateString = await Deno.readTextFile("./views/register.hbs")
const template = HandlebarsEnvironment.compile(templateString)
const body = template()
context.response.body = body
})
router.post('/register', async context => {
console.log('POST /register')
const body = context.request.body({ type: 'form' })
const value = await body.value
const obj = Object.fromEntries(value)
console.log(obj)
await register(obj)
context.response.redirect('/login')
})
router.get('/logout', async context => {
await context.cookies.delete('authorised')
context.response.redirect('/')
})
router.post('/login', async context => {
console.log('POST /login')
const body = context.request.body({ type: 'form' })
const value = await body.value
const obj = Object.fromEntries(value)
console.log(obj)
try {
const username = await login(obj)
await context.cookies.set('authorised', username)
context.response.redirect('/foo')
} catch(err) {
console.log(err)
context.response.redirect('/login')
}
})
router.get('/foo', async context => {
const authorised = context.cookies.get('authorised')
if(authorised === undefined) context.response.redirect('/')
const data = { authorised }
const templateString = await Deno.readTextFile("./views/foo.hbs")
const template = HandlebarsEnvironment.compile(templateString)
const body = template(data)
context.response.body = body
})
export default router