-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
17 changed files
with
245 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { html } from 'uland-isomorphic' | ||
import { useUser } from '../hooks/useUser.js' | ||
import { useWindow } from '../hooks/useWindow.js' | ||
import { useLSP } from '../hooks/useLSP' | ||
|
||
export function header () { | ||
const { user, loading } = useUser() | ||
const window = useWindow() | ||
const state = useLSP() | ||
|
||
return html` | ||
<nav class="bc-header-nav"> | ||
<div class="bc-header-start"> | ||
<div class="round"> | ||
<span>🥖 </span> | ||
<a href="/">Breadcrum</a> | ||
</div> | ||
${user ? html`<div>(${user.username})</div>` : null} | ||
</div> | ||
<div class="bc-header-end"> | ||
${loading | ||
? html`<div>...</div>` | ||
: !user | ||
? html` | ||
${ | ||
window?.location?.pathname !== '/login/' | ||
? html` | ||
<div> | ||
<a href='/login'>login</a> | ||
</div> | ||
` | ||
: null | ||
} | ||
${ | ||
window?.location?.pathname !== '/login/' && window?.location?.pathname !== '/register/' && !state.disableRegistration | ||
? html` | ||
<div>/</div> | ||
` | ||
: null | ||
} | ||
${ | ||
window?.location?.pathname !== '/register/' && !state.disableRegistration | ||
? html` | ||
<div> | ||
<a href='/register'>register</a> | ||
</div> | ||
` | ||
: null | ||
}` | ||
: html` | ||
<div><a href='/logout'>logout</a></div>` | ||
} | ||
</div> | ||
</nav> | ||
` | ||
} |
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,5 +1,6 @@ | ||
export default async () => { | ||
return { | ||
siteName: 'breadcrum.net' | ||
siteName: 'breadcrum.net', | ||
disableRegistration: true | ||
} | ||
} |
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,12 @@ | ||
import { useEffect, useState } from 'uland-isomorphic' | ||
|
||
export function useWindow () { | ||
const wr = (typeof window !== 'undefined') ? window : null | ||
const [w, setW] = useState(wr) | ||
|
||
useEffect(() => { | ||
setW(wr) | ||
}, [wr]) | ||
|
||
return w | ||
} |
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 +1,91 @@ | ||
console.log('login page') | ||
/* eslint-env browser */ | ||
import { html, render, useState, useEffect } from 'uland-isomorphic' | ||
import { useUser } from '../hooks/useUser.js' | ||
import { useLSP } from '../hooks/useLSP.js' | ||
|
||
export function loginPage () { | ||
const state = useLSP() | ||
const { user, loading, error: userError } = useUser() | ||
const [loggingIn, setLoggingIn] = useState(false) | ||
|
||
useEffect(() => { | ||
if (user && !loading) window.location.replace('/') | ||
}, [user]) | ||
|
||
async function login (ev) { | ||
ev.preventDefault() | ||
setLoggingIn(true) | ||
|
||
const user = ev.currentTarget.user.value | ||
const password = ev.currentTarget.password.value | ||
|
||
try { | ||
const response = await fetch(`${state.apiUrl}/login`, { | ||
method: 'post', | ||
headers: { | ||
'content-type': 'application/json' | ||
}, | ||
body: JSON.stringify({ user, password }), | ||
credentials: 'include' | ||
}) | ||
|
||
if (response.ok && response.status === 201) { | ||
const body = await response.json() | ||
state.user = body?.user | ||
} else { | ||
throw new Error(`${response.status} ${response.statusText} ${await response.text()}`) | ||
} | ||
} catch (err) { | ||
console.log(err) | ||
} finally { | ||
setLoggingIn(false) | ||
} | ||
} | ||
|
||
return html` | ||
${!user | ||
? html` | ||
<div> | ||
<form class="login-form" id="login-form" onsubmit=${login}> | ||
<fieldset ?disabled=${loggingIn}> | ||
<legend>Log in:</legend> | ||
<div> | ||
<label> | ||
Email or Username: | ||
<input type="text" name="user" /> | ||
</label> | ||
</div> | ||
<div> | ||
<label> | ||
Password: | ||
<input type="password" name="password"> | ||
</label> | ||
</div> | ||
<div class="button-cluster"> | ||
<input name="submit-button" type="submit"> | ||
</div> | ||
<div class="error-box"></div> | ||
</fieldset> | ||
</form> | ||
</div> | ||
` | ||
: html` | ||
<div>Logged in as ${user.username}</div> | ||
<div>Redirecting to <a href="/">/</a></button> | ||
` | ||
} | ||
${userError | ||
? html`<div>${JSON.stringify(userError, null, ' ')}</div>` | ||
: null | ||
} | ||
${ | ||
loading | ||
? html`<div>loading</div>` | ||
: null | ||
} | ||
` | ||
} | ||
|
||
if (typeof window !== 'undefined') { | ||
render(document.querySelector('.bc-main'), loginPage) | ||
} |
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,7 +1,8 @@ | ||
import { html } from 'uland-isomorphic' | ||
import { loginPage } from './client.js' | ||
|
||
export default async function () { | ||
return html` | ||
<div>login page page</div> | ||
${loginPage()} | ||
` | ||
} |
This file was deleted.
Oops, something went wrong.
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,46 @@ | ||
/* eslint-env browser */ | ||
import { html, render, useEffect } from 'uland-isomorphic' | ||
import { useUser } from '../hooks/useUser.js' | ||
import { useLSP } from '../hooks/useLSP.js' | ||
|
||
export function logout () { | ||
const state = useLSP() | ||
const { user } = useUser() | ||
|
||
useEffect(() => { | ||
const logout = async () => { | ||
try { | ||
await fetch(`${state.apiUrl}/logout`, { | ||
method: 'post', | ||
credentials: 'include' | ||
}) | ||
} finally { | ||
state.user = null | ||
window.location.replace('/') | ||
} | ||
} | ||
|
||
logout().catch(err => { | ||
console.error(err) | ||
}) | ||
}, []) | ||
|
||
return html` | ||
${!user | ||
? html` | ||
<div> | ||
Logged out | ||
</div> | ||
` | ||
: html` | ||
<div> | ||
Logging out of ${user.username} | ||
</div> | ||
` | ||
} | ||
` | ||
} | ||
|
||
if (typeof window !== 'undefined') { | ||
render(document.querySelector('.bc-main'), logout) | ||
} |
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,6 @@ | ||
import { html } from 'uland-isomorphic' | ||
import { logout } from './client.js' | ||
|
||
export default async function () { | ||
return html`${logout()}` | ||
} |
Oops, something went wrong.