refactor(models, store): stronger typing
This commit is contained in:
parent
2871eec4b5
commit
ff2c4c203e
55
src/models/Auth.ts
Normal file
55
src/models/Auth.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import type { RecordAuthResponse } from "pocketbase";
|
||||
import { User, type IUser } from "./User";
|
||||
|
||||
interface IAuthenticatedUser {
|
||||
id: string,
|
||||
name?: string,
|
||||
username: string,
|
||||
email: string,
|
||||
avatarUrl?: string,
|
||||
accessToken: string,
|
||||
refreshToken: string,
|
||||
rawUser?: { [key: string]: any };
|
||||
}
|
||||
|
||||
export interface IOAuthResponse extends RecordAuthResponse<IUser> {
|
||||
token: string,
|
||||
record: IUser,
|
||||
meta?: IAuthenticatedUser
|
||||
}
|
||||
|
||||
class AuthenticatedUser implements IAuthenticatedUser {
|
||||
id: string;
|
||||
name?: string;
|
||||
username: string;
|
||||
email: string;
|
||||
avatarUrl?: string;
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
rawUser?: { [key: string]: any; };
|
||||
|
||||
constructor(from: IAuthenticatedUser) {
|
||||
this.id = from.id;
|
||||
this.name = from.name;
|
||||
this.username = from.username;
|
||||
this.email = from.email;
|
||||
this.avatarUrl = from.avatarUrl;
|
||||
this.accessToken = from.accessToken;
|
||||
this.refreshToken = from.refreshToken;
|
||||
this.rawUser = from.rawUser;
|
||||
}
|
||||
}
|
||||
|
||||
export class OAuthResponse implements IOAuthResponse {
|
||||
token: string;
|
||||
record: User;
|
||||
meta?: AuthenticatedUser;
|
||||
|
||||
constructor(from: RecordAuthResponse<IUser>) {
|
||||
this.token = from.token;
|
||||
this.record = new User(from.record);
|
||||
if(from.meta) {
|
||||
this.meta = new AuthenticatedUser(from.meta as IAuthenticatedUser);
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@ export interface ISimpleUser extends RecordModel {
|
||||
name?: string;
|
||||
avatar?: string;
|
||||
expand?: { [key: string]: any };
|
||||
avatarLink: (pbStore: any) => Observable<string | null>;
|
||||
}
|
||||
|
||||
export interface IUser extends SimpleUser {
|
||||
|
@ -5,6 +5,7 @@ import { computed, ref } from 'vue';
|
||||
|
||||
import { Campaign, type ICampaign, type NewCampaign } from '@/models/Campaign';
|
||||
import { SimpleUser, type IUser } from '@/models/User';
|
||||
import { OAuthResponse } from '@/models/Auth';
|
||||
|
||||
export const usePocketbaseStore = defineStore('pocketbase', () => {
|
||||
const pb = new PocketBase(import.meta.env.VITE_PB_URL);
|
||||
@ -14,20 +15,24 @@ export const usePocketbaseStore = defineStore('pocketbase', () => {
|
||||
// Authentication //
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const authData = ref<RecordAuthResponse<RecordModel> | null>(null);
|
||||
const authData = ref<OAuthResponse | null>(null);
|
||||
const authStore = computed<BaseAuthStore>(() => pb.authStore);
|
||||
const loggedIn = computed<boolean>(() => pb.authStore.isValid);
|
||||
const username = computed<string | null>(() => authStore.value.model?.username);
|
||||
const userId = computed<string | null>(() => authStore.value.model?.id);
|
||||
|
||||
function login(): Observable<RecordAuthResponse<RecordModel>> {
|
||||
return from(pb.collection('users').authWithOAuth2({ provider: 'discord' })).pipe(
|
||||
map((auth) => (authData.value = auth))
|
||||
function login(): Observable<OAuthResponse> {
|
||||
return from(pb.collection('users').authWithOAuth2<IUser>({ provider: 'discord' })).pipe(
|
||||
map((auth) => new OAuthResponse(auth)),
|
||||
tap((auth) => (authData.value = auth))
|
||||
);
|
||||
}
|
||||
|
||||
function refresh(): Observable<RecordAuthResponse<RecordModel>> {
|
||||
return from(pb.collection('users').authRefresh()).pipe(tap((auth) => (authData.value = auth)));
|
||||
return from(pb.collection('users').authRefresh<IUser>()).pipe(
|
||||
map((auth) => new OAuthResponse(auth)),
|
||||
tap((auth) => (authData.value = auth))
|
||||
);
|
||||
}
|
||||
|
||||
function logout() {
|
||||
|
Loading…
Reference in New Issue
Block a user