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=
|
||||
|
||||
# Userinfo route
|
||||
USERINFO__ROUTE=
|
||||
# Identifier
|
||||
USERINFO__IDENTIFIER=
|
||||
USERINFO__ROUTE=
|
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">
|
||||
import "@fontsource-variable/inter";
|
||||
export let data: { userid?: string };
|
||||
export let data: { user?: { sub: string, username: string } };
|
||||
</script>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
@ -32,7 +32,7 @@
|
|||
<nav>
|
||||
<a href="/">Home</a>
|
||||
<a href="/set">Set avatar</a>
|
||||
{#if data.userid}
|
||||
{#if data.user}
|
||||
<a href="/logout">Log out</a>
|
||||
{/if}
|
||||
</nav>
|
||||
|
|
|
@ -6,5 +6,5 @@
|
|||
</p>
|
||||
<p>
|
||||
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>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import configuration from "$lib/configuration.js";
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
|
||||
export function load({}) {
|
||||
throw redirect(301, "/")
|
||||
throw redirect(301, configuration.oauth2.endpoints.logout)
|
||||
}
|
|
@ -1,4 +1,7 @@
|
|||
export function load({ cookies }) {
|
||||
let token = cookies.get("accessToken")
|
||||
|
||||
import {launchLogin} from "$lib"
|
||||
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">
|
||||
export let data: { userid: string };
|
||||
export let data: { user: { sub: string, username: string } };
|
||||
</script>
|
||||
|
||||
<h1>Set an avatar</h1>
|
||||
<h1>Hi, {data.user.username}</h1>
|
||||
<p>
|
||||
Your identifier is {data.userid}.
|
||||
Your identifier is {data.user.sub}.
|
||||
</p>
|
Loading…
Reference in a new issue