make ava redirect to the authentication page
This commit is contained in:
parent
dc6ba5f2c1
commit
c61a32369c
|
@ -11,6 +11,4 @@ OAUTH2__CLIENT_ID=
|
||||||
OAUTH2__CLIENT_SECRET=
|
OAUTH2__CLIENT_SECRET=
|
||||||
|
|
||||||
# Userinfo route
|
# Userinfo route
|
||||||
USERINFO__ROUTE=
|
USERINFO__ROUTE=
|
||||||
# Identifier
|
|
||||||
USERINFO__IDENTIFIER=
|
|
17
src/lib/configuration.ts
Normal file
17
src/lib/configuration.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
const configuration = {
|
||||||
|
oauth2: {
|
||||||
|
endpoints: {
|
||||||
|
authenticate: process.env.OAUTH2__AUTHENTICATE,
|
||||||
|
logout: process.env.OAUTH2__LOGOUT,
|
||||||
|
token: process.env.OAUTH2__TOKEN
|
||||||
|
},
|
||||||
|
client: {
|
||||||
|
id: process.env.OAUTH2__CLIENT_ID,
|
||||||
|
secret: process.env.OAUTH2__CLIENT_SECRET
|
||||||
|
}
|
||||||
|
},
|
||||||
|
userinfo: {
|
||||||
|
route: process.env.USERINFO__ROUTE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export default configuration
|
|
@ -1,3 +1,28 @@
|
||||||
export function userAuthorize() {
|
import { redirect } from "@sveltejs/kit"
|
||||||
|
import configuration from "./configuration"
|
||||||
|
|
||||||
|
const states = new Map<string, ReturnType<typeof setTimeout>>()
|
||||||
|
|
||||||
|
export function launchLogin(req: Request) {
|
||||||
|
// Create a state to be used in the OAuth2 authorization request
|
||||||
|
const state = crypto.randomUUID()
|
||||||
|
|
||||||
|
// Generate the query string and construct a URL using it
|
||||||
|
const searchParams = new URLSearchParams({
|
||||||
|
response_type: "code",
|
||||||
|
client_id: configuration.oauth2.client.id,
|
||||||
|
redirect_uri: new URL(`/set`, req.url).toString(),
|
||||||
|
scope: "openid profile email",
|
||||||
|
state
|
||||||
|
})
|
||||||
|
// Did not think this would work lmao
|
||||||
|
const target = new URL(
|
||||||
|
`?${searchParams.toString()}`,
|
||||||
|
configuration.oauth2.endpoints.authenticate
|
||||||
|
)
|
||||||
|
|
||||||
|
states
|
||||||
|
.set(state, setTimeout(() => states.delete(state), 60000))
|
||||||
|
|
||||||
|
return redirect(302, target.toString())
|
||||||
}
|
}
|
3
src/routes/+layout.server.ts
Normal file
3
src/routes/+layout.server.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export async function load({request}) {
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import "@fontsource-variable/inter";
|
import "@fontsource-variable/inter";
|
||||||
export let data: { userid?: string };
|
export let data: { user?: { sub: string, username: string } };
|
||||||
</script>
|
</script>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
@ -32,7 +32,7 @@
|
||||||
<nav>
|
<nav>
|
||||||
<a href="/">Home</a>
|
<a href="/">Home</a>
|
||||||
<a href="/set">Set avatar</a>
|
<a href="/set">Set avatar</a>
|
||||||
{#if data.userid}
|
{#if data.user}
|
||||||
<a href="/logout">Log out</a>
|
<a href="/logout">Log out</a>
|
||||||
{/if}
|
{/if}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
|
@ -6,5 +6,5 @@
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
If you'd like to set a profile picture, <a href="/set">click here</a>.
|
If you'd like to set a profile picture, <a href="/set">click here</a>.
|
||||||
If logged out, you will be redirected to the OAuth2 provider of this instance.
|
If logged out, you will be redirected to the <abbr title="OpenID Connect">OIDC</abbr> provider of this instance.
|
||||||
</p>
|
</p>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
|
import configuration from "$lib/configuration.js";
|
||||||
import { redirect } from "@sveltejs/kit";
|
import { redirect } from "@sveltejs/kit";
|
||||||
|
|
||||||
export function load({}) {
|
export function load({}) {
|
||||||
throw redirect(301, "/")
|
throw redirect(301, configuration.oauth2.endpoints.logout)
|
||||||
}
|
}
|
|
@ -1,4 +1,7 @@
|
||||||
export function load({ cookies }) {
|
import {launchLogin} from "$lib"
|
||||||
let token = cookies.get("accessToken")
|
export async function load({ request, parent }) {
|
||||||
|
//const { user } = await parent();
|
||||||
|
let user = null
|
||||||
|
if (!user)
|
||||||
|
throw launchLogin(request)
|
||||||
}
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export let data: { userid: string };
|
export let data: { user: { sub: string, username: string } };
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>Set an avatar</h1>
|
<h1>Hi, {data.user.username}</h1>
|
||||||
<p>
|
<p>
|
||||||
Your identifier is {data.userid}.
|
Your identifier is {data.user.sub}.
|
||||||
</p>
|
</p>
|
Loading…
Reference in a new issue