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.ts */
import { Router } from 'https://deno.land/x/oak/mod.ts'
import { Handlebars, HandlebarsConfig } from 'https://deno.land/x/handlebars/mod.ts'
import { upload } from 'https://cdn.deno.land/oak_upload_middleware/versions/v2/raw/mod.ts'
// import { parse } from 'https://deno.land/std/flags/mod.ts'
import { login, loginConfig, register, registerConfig } from './modules/accounts.ts'
import { handlebarsConfig } from './modules/util.ts'
const handle = new Handlebars(handlebarsConfig)
const router: Router = new Router()
// the routes defined here
router.get('/', async context => {
const authorised = context.cookies.get('authorised')
const data = { authorised }
const body = await handle.renderView('home', data)
context.response.body = body
})
router.get('/login', async context => {
const body = await handle.renderView('login')
context.response.body = body
})
router.get('/register', async context => {
const body = await handle.renderView('register')
context.response.body = body
})
router.post('/register', async context => {
console.log('POST /register')
const body = context.request.body({ type: 'form' })
console.log(body)
const value = await body.value
console.log(value)
const username: string = String(value.get('user'))
const password: string = String(value.get('pass'))
const password2: string = String(value.get('pass2'))
const credentials: registerConfig = { username, password, password2 }
console.log(credentials)
await register(credentials)
context.response.redirect('/login')
})
router.get('/logout', context => {
// context.cookies.set('authorised', null) // this does the same
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 username = String(value.get('user'))
const password = String(value.get('pass'))
const credentials: loginConfig = { username, password }
console.log(credentials)
try {
await login(credentials)
context.cookies.set('authorised', credentials.username)
context.response.redirect('/foo')
} catch(err) {
console.log('ERROR CAUGHT IN ROUTE')
context.response.redirect('/login')
}
})
router.get('/foo', async context => {
const auth = context.cookies.get('authorised')
if(auth === undefined) context.response.redirect('/')
const data = { auth }
const body = await handle.renderView('foo', data)
context.response.body = body
})
export default router