Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
59 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
.DS-Store | ||
.DS_Store | ||
package-lock.json | ||
*.db | ||
node_modules/ |
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,31 @@ | ||
|
||
export function generateToken(user, pass) { | ||
const token = `${user}:${pass}` | ||
const hash = btoa(token) | ||
return `Basic ${hash}` | ||
} | ||
|
||
export async function login() { | ||
if(!getCookie('authorization')) throw new Error('cookie not found') | ||
const options = { headers: { Authorization: getCookie('authorization') } } | ||
const response = await fetch('/login',options) | ||
const status = response.status | ||
console.log(`HTTP status code: ${status}`) | ||
if(response.status === 401) throw new Error('status 401 NOT AUTHORIZED') | ||
} | ||
|
||
// from plainjs.com | ||
export function setCookie(name, value, days) { | ||
const d = new Date | ||
d.setTime(d.getTime() + 24*60*60*1000*days) | ||
document.cookie = `${name}=${value};path=/;expires=${d.toGMTString()}` | ||
} | ||
|
||
export function getCookie(name) { | ||
const v = document.cookie.match(`(^|;) ?${name}=([^;]*)(;|$)`) | ||
return v ? v[2] : null | ||
} | ||
|
||
export function deleteCookie(name) { | ||
setCookie(name, '', -1) | ||
} |
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,16 +1,7 @@ | ||
|
||
import { deleteCookie } from '../core.js' | ||
|
||
export async function setup() { | ||
deleteCookie('authorization') | ||
window.location.href = '/#login' | ||
} | ||
|
||
// from plainjs.com | ||
function setCookie(name, value, days) { | ||
const d = new Date | ||
d.setTime(d.getTime() + 24*60*60*1000*days) | ||
document.cookie = `${name}=${value};path=/;expires=${d.toGMTString()}` | ||
} | ||
|
||
function deleteCookie(name) { | ||
setCookie(name, '', -1) | ||
} |
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,16 +1,22 @@ | ||
|
||
export async function setup() { | ||
document.querySelector('h1').innerText = 'Register a New Account' | ||
document.querySelector('form').addEventListener('submit', async event => { | ||
event.preventDefault() | ||
document.querySelector('form').addEventListener('submit', await registerAccount) | ||
} | ||
|
||
async function registerAccount(event) { | ||
event.preventDefault() | ||
try { | ||
const elements = [...document.forms['register'].elements] | ||
const data = {} | ||
elements.forEach( el => { if(el.name) data[el.name] = el.value }) | ||
console.log(data) | ||
const options = { method: 'post', body: JSON.stringify(data) } | ||
const response = await fetch('/register',options) | ||
const json = await response.json() | ||
const status = response.status | ||
if(status === 201) window.location.href = '/#login' | ||
}) | ||
if(response.status === 422) throw new Error('422 Unprocessable Entity') | ||
window.location.href = '/#login' | ||
} catch(err) { | ||
console.log(err.message) | ||
} | ||
} |