-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathme-auth-guard.ts
More file actions
76 lines (66 loc) · 1.91 KB
/
Copy pathme-auth-guard.ts
File metadata and controls
76 lines (66 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { consume } from '@lit/context'
import { css, html, LitElement, nothing } from 'lit'
import { customElement } from 'lit/decorators.js'
import { authContext } from '../../../contexts/auth-context.js'
import { RepositoryObserver } from '../../../controllers/RepositoryObserver.js'
import type { IAuthRepository } from '../../../domain/AuthRepository.js'
/**
* A declarative component that protects its content based on authentication status.
*/
@customElement('me-auth-guard')
export class MeAuthGuard extends LitElement {
@consume({ context: authContext, subscribe: true })
set authRepo(repo: IAuthRepository) {
if (this._authRepo === repo) return
this._authRepo = repo
this._observer?.disconnect()
if (repo) this._observer = new RepositoryObserver(this, repo)
}
get authRepo() {
return this._authRepo
}
private _authRepo!: IAuthRepository
private _observer?: RepositoryObserver
connectedCallback() {
super.connectedCallback()
this.checkSession()
}
private async checkSession() {
if (this.authRepo?.status === 'unknown') {
await this.authRepo.refreshSession()
}
}
render() {
const status = this.authRepo?.status
if (status === 'checking' || status === 'unknown') {
return html`
<div class="guard-status">
<p>認証状態を確認しています...</p>
</div>
`
}
if (status === 'authenticated') {
return html`<slot></slot>`
}
// Guest or failed - render nothing, parent orchestrator will handle redirect
return nothing
}
static styles = css`
:host {
display: contents;
}
.guard-status {
min-height: 60dvh;
display: grid;
place-items: center;
color: var(--color-text-secondary);
font-size: 15px;
letter-spacing: var(--tracking-wide);
}
`
}
declare global {
interface HTMLElementTagNameMap {
'me-auth-guard': MeAuthGuard
}
}