Skip to content

Commit

Permalink
fix: Define type for user does nothing #13
Browse files Browse the repository at this point in the history
  • Loading branch information
trandaison committed May 31, 2024
1 parent 8fefe21 commit 51f3ce0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
8 changes: 5 additions & 3 deletions docs/guide/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ To fully type the `user` object, you can redefine the type for the `user` object

```ts
// ts-shim.d.ts
export type MyUser = {
import type { User } from '@trandaison/nuxt-3-auth';

export type CustomUser = {
id: number;
name: string;
email: string;
Expand All @@ -25,8 +27,8 @@ export type MyUser = {
// ...
};

declare module 'nuxt-3-auth/types' {
interface User extends MyUser {}
declare module '@trandaison/nuxt-3-auth' {
interface User extends CustomUser {}
}

export {};
Expand Down
8 changes: 5 additions & 3 deletions docs/vi/guide/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Nghĩa là `user` object có thể chứa bất kỳ property nào, điều này

```ts
// ts-shim.d.ts
export type MyUser = {
import type { User } from '@trandaison/nuxt-3-auth';

export type CustomUser = {
id: number;
name: string;
email: string;
Expand All @@ -25,8 +27,8 @@ export type MyUser = {
// ...
};

declare module 'nuxt-3-auth/types' {
interface User extends MyUser {}
declare module '@trandaison/nuxt-3-auth' {
interface User extends CustomUser {}
}

export {};
Expand Down
2 changes: 2 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import { generate as generateUseLocalizeRouteTemplate } from "./templates/composables/useLocalizeRoute.template";
import type { AuthOptions, AuthConfig } from "./types";

export * from "./types";

declare module "@nuxt/schema" {
interface ConfigSchema {
publicRuntimeConfig?: {
Expand Down
17 changes: 9 additions & 8 deletions src/runtime/services/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import type {
} from "../../types";
import AuthStorage from './AuthStorage';
import HttpService from './HttpService';
import type { Store } from 'pinia';
import type { Actions, Getters, State } from '../store/auth';
import type { Store } from "pinia";
import type { Ref } from "vue";
import type { Actions, Getters, State } from "../store/auth";

export class Auth implements AuthService {
public config!: AuthConfig;
Expand All @@ -30,7 +31,7 @@ export class Auth implements AuthService {
this.storage = new AuthStorage({ authConfig: auth });
}

get user() {
get user(): Ref<User | null> {
return this.storage.user;
}

Expand All @@ -48,15 +49,15 @@ export class Auth implements AuthService {
return redirectPath;
}

get accessToken() {
get accessToken(): string | null {
return this.storage.accessToken;
}

get refreshToken() {
get refreshToken(): string | null {
return this.storage.refreshToken;
}

get loggedIn() {
get loggedIn(): Ref<boolean> {
return this.storage.loggedIn;
}

Expand Down Expand Up @@ -157,8 +158,8 @@ export class Auth implements AuthService {
);
const res = await this.refreshTokensPromise;
const data = this.getProperty(res, "refresh");
const token = this.getToken(data, "token");
const refresh_token = this.getToken(data, "refreshToken");
const token = this.getToken(data, "token") as string;
const refresh_token = this.getToken(data, "refreshToken") as string;
this.storage.setAuth({ token, refresh_token });
return { token, refresh_token };
} catch (error) {
Expand Down
14 changes: 7 additions & 7 deletions src/types.d.ts → src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Store } from '@pinia/nuxt/dist/runtime/composables';
import type { Ref } from 'vue';
import type { Actions, Getters, State } from './runtime/store/auth';
import HttpService from './runtime/services/HttpService';
import AuthStorage from './runtime/services/AuthStorage';
import type { Store } from "pinia";
import type { Ref } from "vue";
import type { Actions, Getters, State } from "./runtime/store/auth";
import HttpService from "./runtime/services/HttpService";
import AuthStorage from "./runtime/services/AuthStorage";

declare module "#app" {
interface PageMeta {
Expand Down Expand Up @@ -123,8 +123,8 @@ export interface AuthService {
setReferer(url: string | null): void;
}

export type AuthStatus = 'unauthorized' | 'expired';
export type AuthStatus = "unauthorized" | "expired";

export type AuthPageMeta = 'guest' | boolean;
export type AuthPageMeta = "guest" | boolean;

export type AuthFetchOption = "optional" | boolean;

0 comments on commit 51f3ce0

Please sign in to comment.