Actually do import the token into the auth app, fix applicationSchema and authAppSchema

This commit is contained in:
Alex Gleason 2024-10-23 12:58:27 -05:00
parent ae546db8f0
commit 09f813403f
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 28 additions and 6 deletions

View File

@ -15,6 +15,7 @@ import {
SWITCH_ACCOUNT,
VERIFY_CREDENTIALS_SUCCESS,
VERIFY_CREDENTIALS_FAIL,
AUTH_APP_AUTHORIZED,
} from '../actions/auth';
import { ME_FETCH_SKIP } from '../actions/me';
@ -143,6 +144,18 @@ function reducer(state: SoapboxAuth, action: UnknownAction): SoapboxAuth {
const result = applicationSchema.safeParse(action.app);
return result.success ? importApplication(state, result.data) : state;
}
case AUTH_APP_AUTHORIZED: {
const result = tokenSchema.safeParse(action.token);
if (result.success) {
return produce(state, draft => {
if (draft.app) {
draft.app.access_token = result.data.access_token;
}
});
} else {
return state;
}
}
case AUTH_LOGGED_IN: {
const result = tokenSchema.safeParse(action.token);
return result.success ? importToken(state, result.data) : state;

View File

@ -1,18 +1,19 @@
import { z } from 'zod';
const applicationSchema = z.object({
name: z.string(),
name: z.string().catch(''),
website: z.string().url().nullable().catch(null),
scopes: z.string().array().catch([]),
redirect_uris: z.string().url().array().optional().catch(undefined),
redirect_uri: z.string().url().optional().catch(undefined),
client_id: z.string().optional().catch(undefined),
client_secret: z.string().optional().catch(undefined),
client_secret_expires_at: z.number().optional().catch(0),
}).transform((app) => {
const { name, website, scopes, redirect_uris, redirect_uri } = app;
const { redirect_uris, redirect_uri, ...rest } = app;
return {
name,
website,
scopes,
...rest,
redirect_uris: redirect_uris || (redirect_uri ? [redirect_uri] : []),
};
});

View File

@ -9,8 +9,14 @@ const authUserSchema = z.object({
url: z.string().url(),
});
const authAppSchema = applicationSchema.and(
z.object({
access_token: z.string().optional().catch(undefined),
}),
);
const soapboxAuthSchema = z.object({
app: applicationSchema.optional(),
app: authAppSchema.optional(),
tokens: z.record(z.string(), tokenSchema),
users: z.record(z.string(), authUserSchema),
me: z.string().url().optional(),

View File

@ -5,6 +5,8 @@ const tokenSchema = z.object({
token_type: z.string(),
scope: z.string(),
created_at: z.number(),
id: z.coerce.string().optional().catch(undefined), // Pleroma (primary key)
me: z.string().url().optional().catch(undefined), // Pleroma (ActivityPub ID of user)
});
type Token = z.infer<typeof tokenSchema>;