feat: misskey oauth (fixes #26)
This commit is contained in:
		
							parent
							
								
									ef6aa3ee5f
								
							
						
					
					
						commit
						987ff47704
					
				
					 6 changed files with 516 additions and 10 deletions
				
			
		|  | @ -0,0 +1,38 @@ | |||
| import type { APIError, MeUser } from "$lib/api/entities"; | ||||
| import { apiFetch } from "$lib/api/fetch"; | ||||
| import type { PageServerLoad } from "./$types"; | ||||
| 
 | ||||
| export const load = (async ({ url, params }) => { | ||||
|   try { | ||||
|     const resp = await apiFetch<CallbackResponse>("/auth/misskey/callback", { | ||||
|       method: "POST", | ||||
|       body: { | ||||
|         instance: params.instance, | ||||
|         code: url.searchParams.get("code"), | ||||
|         state: url.searchParams.get("state"), | ||||
|       }, | ||||
|     }); | ||||
| 
 | ||||
|     return { | ||||
|       ...resp, | ||||
|       instance: params.instance, | ||||
|     }; | ||||
|   } catch (e) { | ||||
|     return { error: e as APIError }; | ||||
|   } | ||||
| }) satisfies PageServerLoad; | ||||
| 
 | ||||
| interface CallbackResponse { | ||||
|   has_account: boolean; | ||||
|   token?: string; | ||||
|   user?: MeUser; | ||||
| 
 | ||||
|   fediverse?: string; | ||||
|   ticket?: string; | ||||
|   require_invite: boolean; | ||||
| 
 | ||||
|   is_deleted: boolean; | ||||
|   deleted_at?: string; | ||||
|   self_delete?: boolean; | ||||
|   delete_reason?: string; | ||||
| } | ||||
|  | @ -0,0 +1,66 @@ | |||
| <script lang="ts"> | ||||
|   import { goto } from "$app/navigation"; | ||||
|   import type { APIError, MeUser } from "$lib/api/entities"; | ||||
|   import { apiFetch, apiFetchClient } from "$lib/api/fetch"; | ||||
|   import { userStore } from "$lib/store"; | ||||
|   import type { PageData } from "./$types"; | ||||
|   import { addToast } from "$lib/toast"; | ||||
|   import CallbackPage from "../../CallbackPage.svelte"; | ||||
|   import type { SignupResponse } from "$lib/api/responses"; | ||||
| 
 | ||||
|   export let data: PageData; | ||||
| 
 | ||||
|   const signupForm = async (username: string, invite: string) => { | ||||
|     try { | ||||
|       const resp = await apiFetch<SignupResponse>("/auth/misskey/signup", { | ||||
|         method: "POST", | ||||
|         body: { | ||||
|           instance: data.instance, | ||||
|           ticket: data.ticket, | ||||
|           username: username, | ||||
|           invite_code: invite, | ||||
|         }, | ||||
|       }); | ||||
| 
 | ||||
|       localStorage.setItem("pronouns-token", resp.token); | ||||
|       localStorage.setItem("pronouns-user", JSON.stringify(resp.user)); | ||||
|       userStore.set(resp.user); | ||||
|       addToast({ header: "Welcome!", body: "Signed up successfully!" }); | ||||
|       goto("/"); | ||||
|     } catch (e) { | ||||
|       data.error = e as APIError; | ||||
|     } | ||||
|   }; | ||||
| 
 | ||||
|   const linkAccount = async () => { | ||||
|     try { | ||||
|       const resp = await apiFetchClient<MeUser>("/auth/misskey/add-provider", "POST", { | ||||
|         instance: data.instance, | ||||
|         ticket: data.ticket, | ||||
|       }); | ||||
| 
 | ||||
|       localStorage.setItem("pronouns-user", JSON.stringify(resp)); | ||||
|       userStore.set(resp); | ||||
|       addToast({ header: "Linked account", body: "Successfully linked account!" }); | ||||
|       await goto("/settings/auth"); | ||||
|     } catch (e) { | ||||
|       data.error = e as APIError; | ||||
|     } | ||||
|   }; | ||||
| </script> | ||||
| 
 | ||||
| <CallbackPage | ||||
|   authType="Fediverse" | ||||
|   remoteName="{data.fediverse}@{data.instance}" | ||||
|   error={data.error} | ||||
|   requireInvite={data.require_invite} | ||||
|   isDeleted={data.is_deleted} | ||||
|   ticket={data.ticket} | ||||
|   token={data.token} | ||||
|   user={data.user} | ||||
|   deletedAt={data.deleted_at} | ||||
|   selfDelete={data.self_delete} | ||||
|   deleteReason={data.delete_reason} | ||||
|   {linkAccount} | ||||
|   {signupForm} | ||||
| /> | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue