@rapid-rn/plugin-auth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +188 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +147 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
- package/templates/AuthGuard.tsx.hbs +110 -0
- package/templates/AuthHeader.tsx.hbs +91 -0
- package/templates/ForgotPasswordScreen.tsx.hbs +204 -0
- package/templates/LoginScreen.tsx.hbs +227 -0
- package/templates/OTPScreen.tsx.hbs +288 -0
- package/templates/SignupScreen.tsx.hbs +272 -0
- package/templates/auth.service.ts.hbs +158 -0
- package/templates/auth.store.ts.hbs +117 -0
- package/templates/index.ts.hbs +7 -0
- package/templates/useAuth.ts.hbs +137 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { apiClient } from "@services/api";
|
|
2
|
+
|
|
3
|
+
// ─── Request / Response Types ─────────────────────────────────────────────────
|
|
4
|
+
// Customise these interfaces to match your backend API contracts.
|
|
5
|
+
|
|
6
|
+
export interface LoginRequest {
|
|
7
|
+
email: string;
|
|
8
|
+
password: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SignupRequest {
|
|
12
|
+
name: string;
|
|
13
|
+
email: string;
|
|
14
|
+
password: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface AuthResponse {
|
|
18
|
+
accessToken: string;
|
|
19
|
+
refreshToken: string;
|
|
20
|
+
user: {
|
|
21
|
+
id: string;
|
|
22
|
+
email: string;
|
|
23
|
+
name: string;
|
|
24
|
+
role?: string;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface RefreshResponse {
|
|
29
|
+
accessToken: string;
|
|
30
|
+
refreshToken: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ForgotPasswordResponse {
|
|
34
|
+
message: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface VerifyOTPResponse {
|
|
38
|
+
resetToken: string;
|
|
39
|
+
verified: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ResetPasswordResponse {
|
|
43
|
+
message: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ─── API Base Path ────────────────────────────────────────────────────────────
|
|
47
|
+
// Change this to match your backend's auth route prefix (e.g. "/api/v1/auth").
|
|
48
|
+
const BASE_PATH = "/auth";
|
|
49
|
+
|
|
50
|
+
// ─── Auth Service ─────────────────────────────────────────────────────────────
|
|
51
|
+
// Each method ships with:
|
|
52
|
+
// • A working dummy implementation (simulates network delay, returns mock data)
|
|
53
|
+
// • A commented-out real API call ready to uncomment when connecting your backend
|
|
54
|
+
//
|
|
55
|
+
// To go live: uncomment the apiClient line, remove the dummy block below it.
|
|
56
|
+
|
|
57
|
+
export const authService = {
|
|
58
|
+
/**
|
|
59
|
+
* Login with email and password.
|
|
60
|
+
* @customise Uncomment: const res = await apiClient.post<AuthResponse>(`${BASE_PATH}/login`, body);
|
|
61
|
+
*/
|
|
62
|
+
login: async (body: LoginRequest): Promise<AuthResponse> => {
|
|
63
|
+
// ── Real API (uncomment when ready) ─────────────────────────────────────
|
|
64
|
+
// const res = await apiClient.post<AuthResponse>(`${BASE_PATH}/login`, body);
|
|
65
|
+
// return res.data;
|
|
66
|
+
|
|
67
|
+
// ── Dummy implementation ─────────────────────────────────────────────────
|
|
68
|
+
await new Promise((r) => setTimeout(r, 800));
|
|
69
|
+
if (body.email && body.password) {
|
|
70
|
+
return {
|
|
71
|
+
accessToken: "dummy-access-token-" + Date.now(),
|
|
72
|
+
refreshToken: "dummy-refresh-token-" + Date.now(),
|
|
73
|
+
user: { id: "1", email: body.email, name: "Test User", role: "user" },
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
throw new Error("Invalid credentials. Please check your email and password.");
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Register a new account.
|
|
81
|
+
* @customise Uncomment: const res = await apiClient.post<AuthResponse>(`${BASE_PATH}/signup`, body);
|
|
82
|
+
*/
|
|
83
|
+
signup: async (body: SignupRequest): Promise<AuthResponse> => {
|
|
84
|
+
// const res = await apiClient.post<AuthResponse>(`${BASE_PATH}/signup`, body);
|
|
85
|
+
// return res.data;
|
|
86
|
+
|
|
87
|
+
await new Promise((r) => setTimeout(r, 800));
|
|
88
|
+
return {
|
|
89
|
+
accessToken: "dummy-access-token-" + Date.now(),
|
|
90
|
+
refreshToken: "dummy-refresh-token-" + Date.now(),
|
|
91
|
+
user: { id: "2", email: body.email, name: body.name, role: "user" },
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Send OTP / reset link to the given email address.
|
|
97
|
+
* @customise Uncomment: const res = await apiClient.post<ForgotPasswordResponse>(`${BASE_PATH}/forgot-password`, body);
|
|
98
|
+
*/
|
|
99
|
+
forgotPassword: async (email: string): Promise<ForgotPasswordResponse> => {
|
|
100
|
+
// const res = await apiClient.post<ForgotPasswordResponse>(`${BASE_PATH}/forgot-password`, { email });
|
|
101
|
+
// return res.data;
|
|
102
|
+
|
|
103
|
+
await new Promise((r) => setTimeout(r, 700));
|
|
104
|
+
return { message: `A verification code has been sent to ${email}` };
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Verify a 6-digit OTP and receive a short-lived reset token.
|
|
109
|
+
* Dummy OTP: 123456
|
|
110
|
+
* @customise Uncomment: const res = await apiClient.post<VerifyOTPResponse>(`${BASE_PATH}/verify-otp`, body);
|
|
111
|
+
*/
|
|
112
|
+
verifyOTP: async (email: string, otp: string): Promise<VerifyOTPResponse> => {
|
|
113
|
+
// const res = await apiClient.post<VerifyOTPResponse>(`${BASE_PATH}/verify-otp`, { email, otp });
|
|
114
|
+
// return res.data;
|
|
115
|
+
|
|
116
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
117
|
+
if (otp === "123456") {
|
|
118
|
+
return { resetToken: "reset-token-" + Date.now(), verified: true };
|
|
119
|
+
}
|
|
120
|
+
throw new Error("Invalid OTP. Hint: use 123456 in demo mode.");
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Set a new password using the reset token from OTP verification.
|
|
125
|
+
* @customise Uncomment: const res = await apiClient.post<ResetPasswordResponse>(`${BASE_PATH}/reset-password`, body);
|
|
126
|
+
*/
|
|
127
|
+
resetPassword: async (resetToken: string, password: string): Promise<ResetPasswordResponse> => {
|
|
128
|
+
// const res = await apiClient.post<ResetPasswordResponse>(`${BASE_PATH}/reset-password`, { resetToken, password });
|
|
129
|
+
// return res.data;
|
|
130
|
+
|
|
131
|
+
await new Promise((r) => setTimeout(r, 600));
|
|
132
|
+
return { message: "Password reset successfully. Please log in with your new password." };
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Refresh the access token using the stored refresh token.
|
|
137
|
+
* Called automatically by the Axios error interceptor on 401 responses — no manual usage needed.
|
|
138
|
+
* @customise Uncomment: const res = await apiClient.post<RefreshResponse>(`${BASE_PATH}/refresh`, { refreshToken });
|
|
139
|
+
*/
|
|
140
|
+
refresh: async (refreshToken: string): Promise<RefreshResponse> => {
|
|
141
|
+
// const res = await apiClient.post<RefreshResponse>(`${BASE_PATH}/refresh`, { refreshToken });
|
|
142
|
+
// return res.data;
|
|
143
|
+
|
|
144
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
145
|
+
return {
|
|
146
|
+
accessToken: "refreshed-access-token-" + Date.now(),
|
|
147
|
+
refreshToken,
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Server-side logout — invalidates the refresh token.
|
|
153
|
+
* @customise Uncomment: await apiClient.post(`${BASE_PATH}/logout`);
|
|
154
|
+
*/
|
|
155
|
+
logout: async (): Promise<void> => {
|
|
156
|
+
// await apiClient.post(`${BASE_PATH}/logout`);
|
|
157
|
+
},
|
|
158
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { create } from "zustand";
|
|
2
|
+
import { storageService } from "@services/storage";
|
|
3
|
+
|
|
4
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
5
|
+
// Extend the User interface to match your backend's user object shape.
|
|
6
|
+
|
|
7
|
+
export interface User {
|
|
8
|
+
id: string;
|
|
9
|
+
email: string;
|
|
10
|
+
name: string;
|
|
11
|
+
role?: string;
|
|
12
|
+
avatar?: string;
|
|
13
|
+
[key: string]: unknown; // Allow extra fields from your backend
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface AuthState {
|
|
17
|
+
// ── State ─────────────────────────────────────────────────────────────────
|
|
18
|
+
accessToken: string | null;
|
|
19
|
+
refreshToken: string | null;
|
|
20
|
+
user: User | null;
|
|
21
|
+
isAuthenticated: boolean;
|
|
22
|
+
/** True while restoring tokens from storage on app start. */
|
|
23
|
+
isInitializing: boolean;
|
|
24
|
+
|
|
25
|
+
// ── Actions ───────────────────────────────────────────────────────────────
|
|
26
|
+
/** Persist auth session after a successful login / signup. */
|
|
27
|
+
setAuth: (accessToken: string, refreshToken: string | null, user: User) => Promise<void>;
|
|
28
|
+
/** Update tokens only (used by the refresh token interceptor). */
|
|
29
|
+
setTokens: (accessToken: string, refreshToken: string | null) => Promise<void>;
|
|
30
|
+
/** Merge partial user data without clearing auth state. */
|
|
31
|
+
updateUser: (partial: Partial<User>) => void;
|
|
32
|
+
/** Clear session and remove persisted tokens. */
|
|
33
|
+
logout: () => Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Restore auth state from persistent storage.
|
|
36
|
+
* Call this once on app start (already wired into _layout.tsx alongside the splash timer).
|
|
37
|
+
*/
|
|
38
|
+
initializeAuth: () => Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ── Storage keys — centralised so interceptors can reuse the same keys ─────────
|
|
42
|
+
export const AUTH_STORAGE_KEYS = {
|
|
43
|
+
ACCESS_TOKEN: "auth_access_token",
|
|
44
|
+
REFRESH_TOKEN: "auth_refresh_token",
|
|
45
|
+
USER: "auth_user",
|
|
46
|
+
} as const;
|
|
47
|
+
|
|
48
|
+
export const useAuthStore = create<AuthState>((set, get) => ({
|
|
49
|
+
// ── Default state ───────────────────────────────────────────────────────────
|
|
50
|
+
accessToken: null,
|
|
51
|
+
refreshToken: null,
|
|
52
|
+
user: null,
|
|
53
|
+
isAuthenticated: false,
|
|
54
|
+
isInitializing: true,
|
|
55
|
+
|
|
56
|
+
// ── setAuth ─────────────────────────────────────────────────────────────────
|
|
57
|
+
setAuth: async (accessToken, refreshToken, user) => {
|
|
58
|
+
await Promise.all([
|
|
59
|
+
storageService.setItem(AUTH_STORAGE_KEYS.ACCESS_TOKEN, accessToken),
|
|
60
|
+
refreshToken
|
|
61
|
+
? storageService.setItem(AUTH_STORAGE_KEYS.REFRESH_TOKEN, refreshToken)
|
|
62
|
+
: Promise.resolve(),
|
|
63
|
+
storageService.setItem(AUTH_STORAGE_KEYS.USER, JSON.stringify(user)),
|
|
64
|
+
]);
|
|
65
|
+
set({ accessToken, refreshToken, user, isAuthenticated: true });
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
// ── setTokens ───────────────────────────────────────────────────────────────
|
|
69
|
+
setTokens: async (accessToken, refreshToken) => {
|
|
70
|
+
await Promise.all([
|
|
71
|
+
storageService.setItem(AUTH_STORAGE_KEYS.ACCESS_TOKEN, accessToken),
|
|
72
|
+
refreshToken
|
|
73
|
+
? storageService.setItem(AUTH_STORAGE_KEYS.REFRESH_TOKEN, refreshToken)
|
|
74
|
+
: Promise.resolve(),
|
|
75
|
+
]);
|
|
76
|
+
set({ accessToken, refreshToken });
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
// ── updateUser ──────────────────────────────────────────────────────────────
|
|
80
|
+
updateUser: (partial) => {
|
|
81
|
+
const current = get().user;
|
|
82
|
+
if (current) {
|
|
83
|
+
set({ user: { ...current, ...partial } });
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
// ── logout ──────────────────────────────────────────────────────────────────
|
|
88
|
+
logout: async () => {
|
|
89
|
+
await Promise.all([
|
|
90
|
+
storageService.removeItem(AUTH_STORAGE_KEYS.ACCESS_TOKEN),
|
|
91
|
+
storageService.removeItem(AUTH_STORAGE_KEYS.REFRESH_TOKEN),
|
|
92
|
+
storageService.removeItem(AUTH_STORAGE_KEYS.USER),
|
|
93
|
+
]);
|
|
94
|
+
set({ accessToken: null, refreshToken: null, user: null, isAuthenticated: false });
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
// ── initializeAuth ──────────────────────────────────────────────────────────
|
|
98
|
+
initializeAuth: async () => {
|
|
99
|
+
// Guard: skip if already initialized
|
|
100
|
+
if (!get().isInitializing) return;
|
|
101
|
+
try {
|
|
102
|
+
const [accessToken, refreshToken, userStr] = await Promise.all([
|
|
103
|
+
storageService.getItem(AUTH_STORAGE_KEYS.ACCESS_TOKEN),
|
|
104
|
+
storageService.getItem(AUTH_STORAGE_KEYS.REFRESH_TOKEN),
|
|
105
|
+
storageService.getItem(AUTH_STORAGE_KEYS.USER),
|
|
106
|
+
]);
|
|
107
|
+
const user: User | null = userStr ? (JSON.parse(userStr) as User) : null;
|
|
108
|
+
if (accessToken) {
|
|
109
|
+
set({ accessToken, refreshToken, user, isAuthenticated: true });
|
|
110
|
+
}
|
|
111
|
+
} catch {
|
|
112
|
+
set({ accessToken: null, refreshToken: null, user: null, isAuthenticated: false });
|
|
113
|
+
} finally {
|
|
114
|
+
set({ isInitializing: false });
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
}));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./screens/LoginScreen";
|
|
2
|
+
export * from "./screens/SignupScreen";
|
|
3
|
+
export * from "./screens/ForgotPasswordScreen";
|
|
4
|
+
export * from "./screens/OTPScreen";
|
|
5
|
+
export * from "./hooks/useAuth";
|
|
6
|
+
export * from "./services/auth.service";
|
|
7
|
+
export * from "./guards/AuthGuard";
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { useAuthStore } from "../store/auth.store";
|
|
3
|
+
import { authService } from "../services/auth.service";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* useAuth — single hook for all auth operations.
|
|
7
|
+
*
|
|
8
|
+
* Provides: login, signup, forgotPassword, verifyOTP, resetPassword, logout
|
|
9
|
+
* Each method manages its own loading / error state.
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* const { login, loading, error } = useAuth();
|
|
13
|
+
* await login({ email, password });
|
|
14
|
+
*/
|
|
15
|
+
export const useAuth = () => {
|
|
16
|
+
const [loading, setLoading] = useState(false);
|
|
17
|
+
const [error, setError] = useState<string | null>(null);
|
|
18
|
+
const { setAuth, logout: storeLogout, isAuthenticated, user } = useAuthStore();
|
|
19
|
+
|
|
20
|
+
const clearError = () => setError(null);
|
|
21
|
+
|
|
22
|
+
// ── login ──────────────────────────────────────────────────────────────────
|
|
23
|
+
const login = async (credentials: { email: string; password: string }): Promise<boolean> => {
|
|
24
|
+
setLoading(true);
|
|
25
|
+
setError(null);
|
|
26
|
+
try {
|
|
27
|
+
const data = await authService.login(credentials);
|
|
28
|
+
await setAuth(data.accessToken, data.refreshToken, data.user);
|
|
29
|
+
return true;
|
|
30
|
+
} catch (err: unknown) {
|
|
31
|
+
const msg = err instanceof Error ? err.message : "Login failed. Please try again.";
|
|
32
|
+
setError(msg);
|
|
33
|
+
return false;
|
|
34
|
+
} finally {
|
|
35
|
+
setLoading(false);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// ── signup ─────────────────────────────────────────────────────────────────
|
|
40
|
+
const signup = async (userData: {
|
|
41
|
+
name: string;
|
|
42
|
+
email: string;
|
|
43
|
+
password: string;
|
|
44
|
+
}): Promise<boolean> => {
|
|
45
|
+
setLoading(true);
|
|
46
|
+
setError(null);
|
|
47
|
+
try {
|
|
48
|
+
const data = await authService.signup(userData);
|
|
49
|
+
await setAuth(data.accessToken, data.refreshToken, data.user);
|
|
50
|
+
return true;
|
|
51
|
+
} catch (err: unknown) {
|
|
52
|
+
const msg = err instanceof Error ? err.message : "Sign up failed. Please try again.";
|
|
53
|
+
setError(msg);
|
|
54
|
+
return false;
|
|
55
|
+
} finally {
|
|
56
|
+
setLoading(false);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// ── forgotPassword ─────────────────────────────────────────────────────────
|
|
61
|
+
const forgotPassword = async (email: string): Promise<boolean> => {
|
|
62
|
+
setLoading(true);
|
|
63
|
+
setError(null);
|
|
64
|
+
try {
|
|
65
|
+
await authService.forgotPassword(email);
|
|
66
|
+
return true;
|
|
67
|
+
} catch (err: unknown) {
|
|
68
|
+
const msg = err instanceof Error ? err.message : "Failed to send reset email.";
|
|
69
|
+
setError(msg);
|
|
70
|
+
return false;
|
|
71
|
+
} finally {
|
|
72
|
+
setLoading(false);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// ── verifyOTP ──────────────────────────────────────────────────────────────
|
|
77
|
+
const verifyOTP = async (
|
|
78
|
+
email: string,
|
|
79
|
+
otp: string
|
|
80
|
+
): Promise<{ resetToken: string } | null> => {
|
|
81
|
+
setLoading(true);
|
|
82
|
+
setError(null);
|
|
83
|
+
try {
|
|
84
|
+
const data = await authService.verifyOTP(email, otp);
|
|
85
|
+
return { resetToken: data.resetToken };
|
|
86
|
+
} catch (err: unknown) {
|
|
87
|
+
const msg = err instanceof Error ? err.message : "OTP verification failed.";
|
|
88
|
+
setError(msg);
|
|
89
|
+
return null;
|
|
90
|
+
} finally {
|
|
91
|
+
setLoading(false);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// ── resetPassword ──────────────────────────────────────────────────────────
|
|
96
|
+
const resetPassword = async (resetToken: string, password: string): Promise<boolean> => {
|
|
97
|
+
setLoading(true);
|
|
98
|
+
setError(null);
|
|
99
|
+
try {
|
|
100
|
+
await authService.resetPassword(resetToken, password);
|
|
101
|
+
return true;
|
|
102
|
+
} catch (err: unknown) {
|
|
103
|
+
const msg = err instanceof Error ? err.message : "Password reset failed.";
|
|
104
|
+
setError(msg);
|
|
105
|
+
return false;
|
|
106
|
+
} finally {
|
|
107
|
+
setLoading(false);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// ── logout ─────────────────────────────────────────────────────────────────
|
|
112
|
+
const logout = async (): Promise<void> => {
|
|
113
|
+
try {
|
|
114
|
+
await authService.logout(); // server-side invalidation (no-op in dummy mode)
|
|
115
|
+
} catch {
|
|
116
|
+
// Ignore server errors on logout — always clear local session
|
|
117
|
+
} finally {
|
|
118
|
+
await storeLogout();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
// State
|
|
124
|
+
isAuthenticated,
|
|
125
|
+
user,
|
|
126
|
+
loading,
|
|
127
|
+
error,
|
|
128
|
+
// Actions
|
|
129
|
+
login,
|
|
130
|
+
signup,
|
|
131
|
+
forgotPassword,
|
|
132
|
+
verifyOTP,
|
|
133
|
+
resetPassword,
|
|
134
|
+
logout,
|
|
135
|
+
clearError,
|
|
136
|
+
};
|
|
137
|
+
};
|