-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolidLogin.svelte
More file actions
139 lines (122 loc) · 2.89 KB
/
SolidLogin.svelte
File metadata and controls
139 lines (122 loc) · 2.89 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<script lang="ts">
import { onMount } from 'svelte'
import { getContext } from 'svelte'
import WebIDCheck from '@hexagon6/svelte-solid-webid-check/webid-check.svelte'
import { startLogin, startLogout } from '$lib/solid/auth.svelte.js'
import { isEmpty } from 'ramda'
let {
base = '',
context = 'session',
oidcIssuer = '',
success,
reset,
handleReset,
handleSet,
}: {
base?: string
context?: string
oidcIssuer?: string
success?: Function
reset?: string
handleReset?: Function
handleSet?: Function
} = $props()
let loginEndpoint = $state(oidcIssuer)
let session = getContext(context)
let info = $derived($session.session.info)
const setUrl = ({ oidcEndpoint }) => {
if (typeof handleSet === 'function') {
handleSet(oidcEndpoint)
}
loginEndpoint = oidcEndpoint
}
const resetUrl = () => {
if (typeof handleReset === 'function') {
handleReset()
}
loginEndpoint = undefined
}
/**
* @type {string}
*/
let redirect
onMount(() => {
redirect = new URLSearchParams(window.location.search).get('redirect_url') || '/'
})
</script>
{#snippet resetButton()}
{#if reset}
<button onclick={resetUrl}>{reset}</button>
{/if}
{/snippet}
{#snippet label()}
Enter your webid
{/snippet}
<div class="login-container">
{#if isEmpty($session?.session)}
{#if !loginEndpoint}
<WebIDCheck {label} placeholder="your WebID url" validOIDCEndpoint={setUrl} />
<a href="https://solidproject.org/users/get-a-pod#next-steps" target="_blank" class="hint"
>I don't have a Solid WebID Profile</a
>
{:else}
<button onclick={() => startLogin($session, loginEndpoint, redirect, base)}
>Login with {loginEndpoint}</button
>
{@render resetButton()}
{/if}
{:else if $session}
{#if success && typeof success === 'function'}
<div class="info">
{@render success(info)}
</div>
{/if}
<div>
<button onclick={() => startLogout($session)}>Log out</button>
{@render resetButton()}
</div>
{:else}
<div class="error">
Setup Error:
<div>
no session available (your users should not see this!)
<div>
use
<code>setContext('session', writable({session})</code> in your app or use
the
<code>session</code>
prop)
</div>
</div>
</div>
{/if}
</div>
<style>
.login-container {
display: flex;
flex-direction: column;
align-items: end;
gap: 0.4em;
}
@container (width > 400px) {
.login-container {
flex-direction: row;
align-items: center;
}
}
.login-container a {
font-size: 0.8em;
}
.error {
background: red;
color: white;
padding: 0.4em;
border-radius: 0.3em;
}
.info {
padding: 0 0.4em;
}
:global(button) {
width: fit-content;
}
</style>