-
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
15 changed files
with
226 additions
and
116 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
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,62 +1,106 @@ | ||
import { refs, Sigui } from 'sigui' | ||
import { UserLogin } from '../../api/schemas/user.ts' | ||
import { Sigui } from 'sigui' | ||
import { UserForgot, UserLogin } from '../../api/schemas/user.ts' | ||
import * as actions from '../rpc/login-register.ts' | ||
import { parseForm } from '../util/parse-form.ts' | ||
import { Link } from '../ui/Link.tsx' | ||
import { Input, Label } from '../ui/index.ts' | ||
|
||
export function Login() { | ||
using $ = Sigui() | ||
|
||
const info = $({ | ||
mode: 'login' as 'login' | 'forgot', | ||
forgotEmail: null as null | string, | ||
error: '' | ||
}) | ||
|
||
function onSubmit(ev: Event & { target: HTMLFormElement, submitter: HTMLElement }) { | ||
function submitLogin(ev: Event & { target: HTMLFormElement, submitter: HTMLElement }) { | ||
ev.preventDefault() | ||
const userLogin = parseForm(ev.target, UserLogin) | ||
actions | ||
.login(userLogin) | ||
.then(actions.loginUser) | ||
.catch(err => info.error = err.message) | ||
return false | ||
} | ||
|
||
if (ev.submitter === refs.forgot) { | ||
actions | ||
.forgotPassword(userLogin.nickOrEmail) | ||
.then(() => { | ||
alert('Check your email for a password reset link.') | ||
}) | ||
} | ||
else { | ||
actions | ||
.login(userLogin) | ||
.then(actions.loginUser) | ||
.catch(err => info.error = err.message) | ||
} | ||
function submitForgot(ev: Event & { target: HTMLFormElement, submitter: HTMLElement }) { | ||
ev.preventDefault() | ||
const userForgot = parseForm(ev.target, UserForgot) | ||
actions | ||
.forgotPassword(userForgot.email) | ||
.then(() => { | ||
info.forgotEmail = userForgot.email | ||
}) | ||
return false | ||
} | ||
|
||
return <form method="post" onsubmit={onSubmit}> | ||
<label> | ||
Nick or Email <input | ||
name="nickOrEmail" | ||
required | ||
spellcheck="false" | ||
autocomplete="nickname" | ||
/> | ||
</label> | ||
return <div> | ||
{() => { | ||
switch (info.mode) { | ||
case 'login': | ||
return <form method="post" onsubmit={submitLogin}> | ||
<h2>Login</h2> | ||
|
||
<div class="flex flex-col sm:items-end gap-1"> | ||
<Label text="Nick or Email"> | ||
<Input | ||
name="nickOrEmail" | ||
required | ||
spellcheck="false" | ||
autocomplete="nickname" | ||
/> | ||
</Label> | ||
|
||
<Label text="Password"> | ||
<Input | ||
name="password" | ||
type="password" | ||
required | ||
autocomplete="current-password" | ||
/> | ||
</Label> | ||
|
||
<div class="flex flex-row items-center justify-end gap-2"> | ||
<Link onclick={() => info.mode = 'forgot'}>Forgot password</Link> | ||
<button type="submit">Login</button> | ||
</div> | ||
|
||
<br /> | ||
<span>{() => info.error}</span> | ||
</div> | ||
|
||
<label> | ||
Password <input | ||
name="password" | ||
type="password" | ||
required | ||
autocomplete="current-password" | ||
/> | ||
</label> | ||
</form> | ||
|
||
<br /> | ||
case 'forgot': | ||
if (info.forgotEmail) { | ||
const site = `https://${info.forgotEmail.split('@')[1]}` | ||
return <div>Done! <a href={site}>Check your email</a> for a reset password link.</div> | ||
} | ||
else { | ||
return <form method="post" onsubmit={submitForgot}> | ||
<h2>Forgot Password</h2> | ||
|
||
<button type="submit">Login</button> | ||
<div class="flex flex-col sm:items-end gap-1"> | ||
<Label text="Email"> | ||
<Input | ||
name="email" | ||
type="email" | ||
required | ||
spellcheck="false" | ||
autocomplete="email" | ||
/> | ||
</Label> | ||
|
||
<button ref="forgot" type="submit">Forgot password</button> | ||
<div class="flex flex-row items-center gap-2"> | ||
<button type="submit">Send reset link</button> | ||
<span>or <Link onclick={() => info.mode = 'login'}>login using password</Link></span> | ||
</div> | ||
|
||
{() => info.error} | ||
</form> | ||
<span>{() => info.error}</span> | ||
</div> | ||
</form> | ||
} | ||
} | ||
}} | ||
</div> | ||
} |
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,34 @@ | ||
import { on } from 'utils' | ||
import { whoami } from '../rpc/login-register.ts' | ||
import { state } from '../state.ts' | ||
|
||
export function OAuthLogin() { | ||
function oauthLogin(provider: string) { | ||
const h = 700 | ||
const w = 500 | ||
const x = window.outerWidth / 2 + window.screenX - (w / 2) | ||
const y = window.outerHeight / 2 + window.screenY - (h / 2) | ||
|
||
const url = new URL(`${location.origin}/oauth/popup`) | ||
url.searchParams.set('provider', provider) | ||
const popup = window.open( | ||
url, | ||
'oauth', | ||
`width=${w}, height=${h}, top=${y}, left=${x}` | ||
) | ||
|
||
if (!popup) alert('Something went wrong') | ||
|
||
on(window, 'storage', () => { | ||
popup!.close() | ||
if (localStorage.oauth?.startsWith('complete')) { | ||
whoami().then(user => state.user = user) | ||
} | ||
else { | ||
alert('OAuth failed.\n\nTry logging in using a different method.') | ||
} | ||
}, { once: true }) | ||
} | ||
|
||
return <button onclick={() => oauthLogin('github')}>Proceed with GitHub</button> | ||
} |
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
Oops, something went wrong.