create-astratra-app 1.2.0 → 1.4.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/README.md +71 -0
- package/bin/create-astratra-app.js +9 -0
- package/package.json +3 -2
- package/src/bricks.js +437 -0
- package/src/createProject.js +99 -13
- package/templates/mobile/README.md +70 -0
- package/templates/mobile/app/_layout.tsx +58 -0
- package/templates/mobile/app/dashboard.tsx +73 -0
- package/templates/mobile/app/forgot-password.tsx +64 -0
- package/templates/mobile/app/home.tsx +32 -0
- package/templates/mobile/app/index.tsx +27 -0
- package/templates/mobile/app/login.tsx +118 -0
- package/templates/mobile/app/notifications.tsx +67 -0
- package/templates/mobile/app/onboarding/_layout.tsx +5 -0
- package/templates/mobile/app/onboarding/done.tsx +45 -0
- package/templates/mobile/app/onboarding/index.tsx +52 -0
- package/templates/mobile/app/settings.tsx +95 -0
- package/templates/mobile/app.config.js +37 -0
- package/templates/mobile/babel.config.js +17 -0
- package/templates/mobile/components/AnimatedPressable.tsx +34 -0
- package/templates/mobile/components/BrandBackground.tsx +33 -0
- package/templates/mobile/components/ErrorBoundary.tsx +28 -0
- package/templates/mobile/components/GlassPanel.tsx +67 -0
- package/templates/mobile/components/GridBackground.tsx +35 -0
- package/templates/mobile/components/KeyboardScreen.tsx +81 -0
- package/templates/mobile/components/PasswordEyeIcon.tsx +41 -0
- package/templates/mobile/components/SessionGuard.tsx +29 -0
- package/templates/mobile/components/dashboard/DashboardHeader.tsx +71 -0
- package/templates/mobile/components/dashboard/DashboardReveal.tsx +46 -0
- package/templates/mobile/components/dashboard/KpiBubble.tsx +169 -0
- package/templates/mobile/components/dashboard/MoreButton.tsx +65 -0
- package/templates/mobile/components/dashboard/NotificationBell.tsx +80 -0
- package/templates/mobile/components/dashboard/StaffTabBar.tsx +70 -0
- package/templates/mobile/components/dashboard/ToolCard.tsx +136 -0
- package/templates/mobile/components/glass/LiquidGlassSurface.tsx +266 -0
- package/templates/mobile/components/onboarding/StepScreen.tsx +185 -0
- package/templates/mobile/constants/dashboard.ts +50 -0
- package/templates/mobile/constants/theme.ts +104 -0
- package/templates/mobile/context/AuthContext.tsx +106 -0
- package/templates/mobile/eas.json +9 -0
- package/templates/mobile/env.example +2 -0
- package/templates/mobile/features/routes.ts +23 -0
- package/templates/mobile/gitignore +7 -0
- package/templates/mobile/i18n/config.ts +24 -0
- package/templates/mobile/i18n/locales/en.json +62 -0
- package/templates/mobile/i18n/locales/fr.json +62 -0
- package/templates/mobile/package.json +48 -0
- package/templates/mobile/services/push.ts +67 -0
- package/templates/mobile/services/session.ts +77 -0
- package/templates/mobile/tsconfig.json +8 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who is signed in, for the whole app.
|
|
3
|
+
*
|
|
4
|
+
* The restore-on-launch state is the one worth naming: while the keystore is
|
|
5
|
+
* being read, the answer is neither "signed in" nor "signed out". Treating it
|
|
6
|
+
* as signed out flashes the sign-in screen at every cold start — which is why
|
|
7
|
+
* `loading` exists and why the route guard refuses to decide during it.
|
|
8
|
+
*/
|
|
9
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react';
|
|
10
|
+
import Constants, { ExecutionEnvironment } from 'expo-constants';
|
|
11
|
+
import { router } from 'expo-router';
|
|
12
|
+
import { api, onSessionExpired, session } from '../services/session';
|
|
13
|
+
import { push } from '../services/push';
|
|
14
|
+
|
|
15
|
+
/* See app/_layout.tsx: touching the push module in Expo Go raises an error
|
|
16
|
+
banner, and there is nothing to register there anyway. */
|
|
17
|
+
const IN_EXPO_GO = Constants.executionEnvironment === ExecutionEnvironment.StoreClient;
|
|
18
|
+
|
|
19
|
+
export type AuthUser = { id: string; email: string; role: string; fullName?: string };
|
|
20
|
+
|
|
21
|
+
type AuthValue = {
|
|
22
|
+
user: AuthUser | null;
|
|
23
|
+
loading: boolean;
|
|
24
|
+
signIn: (email: string, password: string) => Promise<void>;
|
|
25
|
+
signOut: () => Promise<void>;
|
|
26
|
+
reload: () => Promise<void>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const AuthContext = createContext<AuthValue | null>(null);
|
|
30
|
+
|
|
31
|
+
export function AuthProvider({ children }: { children: ReactNode }) {
|
|
32
|
+
const [user, setUser] = useState<AuthUser | null>(null);
|
|
33
|
+
const [loading, setLoading] = useState(true);
|
|
34
|
+
|
|
35
|
+
const reload = useCallback(async () => {
|
|
36
|
+
try {
|
|
37
|
+
setUser(await api.request<AuthUser>('/auth/me'));
|
|
38
|
+
} catch {
|
|
39
|
+
setUser(null);
|
|
40
|
+
}
|
|
41
|
+
}, []);
|
|
42
|
+
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
let alive = true;
|
|
45
|
+
onSessionExpired(() => {
|
|
46
|
+
if (!alive) return;
|
|
47
|
+
setUser(null);
|
|
48
|
+
router.replace('/login');
|
|
49
|
+
});
|
|
50
|
+
void (async () => {
|
|
51
|
+
const token = await session.getAccessToken();
|
|
52
|
+
if (token) await reload();
|
|
53
|
+
if (alive) setLoading(false);
|
|
54
|
+
})();
|
|
55
|
+
return () => { alive = false; };
|
|
56
|
+
}, [reload]);
|
|
57
|
+
|
|
58
|
+
const signIn = useCallback(async (email: string, password: string) => {
|
|
59
|
+
const result = await api.request<{ token: string; refreshToken?: string; user: AuthUser }>(
|
|
60
|
+
'/auth/login',
|
|
61
|
+
{ method: 'POST', body: JSON.stringify({ email, password }) }
|
|
62
|
+
);
|
|
63
|
+
/* `refreshToken` is absent unless the API enables it (saas-kit's
|
|
64
|
+
`refreshTokens: { enabled: true }`). Without it the session simply ends
|
|
65
|
+
when the access token expires, and the person signs in again. */
|
|
66
|
+
await session.save({ accessToken: result.token, refreshToken: result.refreshToken });
|
|
67
|
+
/* Signing in re-opens the door that signing out closed. */
|
|
68
|
+
if (!IN_EXPO_GO) {
|
|
69
|
+
push.allowRegistration();
|
|
70
|
+
void push.sync();
|
|
71
|
+
}
|
|
72
|
+
setUser(result.user);
|
|
73
|
+
}, []);
|
|
74
|
+
|
|
75
|
+
/* Push cleanup runs BEFORE the sign-out request and cannot block it: an app
|
|
76
|
+
that refuses to sign out because the network is down is a trap. */
|
|
77
|
+
const signOut = useCallback(async () => {
|
|
78
|
+
const cleanUpPush = IN_EXPO_GO
|
|
79
|
+
? async (signOutRequest: () => Promise<void>) => signOutRequest()
|
|
80
|
+
: push.logout;
|
|
81
|
+
|
|
82
|
+
await cleanUpPush(async () => {
|
|
83
|
+
try {
|
|
84
|
+
await api.request('/auth/logout', { method: 'POST' });
|
|
85
|
+
} catch {
|
|
86
|
+
/* signing out offline stays possible */
|
|
87
|
+
}
|
|
88
|
+
await session.clear();
|
|
89
|
+
});
|
|
90
|
+
setUser(null);
|
|
91
|
+
router.replace('/login');
|
|
92
|
+
}, []);
|
|
93
|
+
|
|
94
|
+
const value = useMemo(
|
|
95
|
+
() => ({ user, loading, signIn, signOut, reload }),
|
|
96
|
+
[user, loading, signIn, signOut, reload]
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function useAuth() {
|
|
103
|
+
const value = useContext(AuthContext);
|
|
104
|
+
if (!value) throw new Error('useAuth must be used inside AuthProvider.');
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cli": { "version": ">= 12.0.0", "appVersionSource": "remote" },
|
|
3
|
+
"build": {
|
|
4
|
+
"development": { "developmentClient": true, "distribution": "internal" },
|
|
5
|
+
"preview": { "distribution": "internal" },
|
|
6
|
+
"production": { "autoIncrement": true }
|
|
7
|
+
},
|
|
8
|
+
"submit": { "production": {} }
|
|
9
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What is reachable without a session, and where a notification may lead.
|
|
3
|
+
*
|
|
4
|
+
* Both lists are written the same way, and it is the important part: they name
|
|
5
|
+
* what is ALLOWED. A list of protected screens silently admits every screen
|
|
6
|
+
* added later and forgotten in the list.
|
|
7
|
+
*/
|
|
8
|
+
import { createRouteGuard } from '@astratra/client';
|
|
9
|
+
import { createNotificationRouter } from '@astratra/native';
|
|
10
|
+
|
|
11
|
+
export const guard = createRouteGuard({
|
|
12
|
+
publicSegments: ['home', 'login', 'forgot-password', 'reset-password', 'onboarding'],
|
|
13
|
+
loginRoute: '/login'
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/** Add a rule per screen a notification may open. Anything else lands on the list. */
|
|
17
|
+
export const notificationRouter = createNotificationRouter({
|
|
18
|
+
fallback: '/notifications',
|
|
19
|
+
routes: [
|
|
20
|
+
{ pattern: /^\/dashboard$/, allow: (role) => Boolean(role) },
|
|
21
|
+
{ pattern: /^\/settings$/, allow: (role) => Boolean(role) }
|
|
22
|
+
]
|
|
23
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The screen's language, decided once.
|
|
3
|
+
*
|
|
4
|
+
* The device locale is the default, and it is only a default: a person whose
|
|
5
|
+
* phone is in English may want the app in French. The choice, once made, is
|
|
6
|
+
* the session's — the API reads it from the Accept-Language header on every
|
|
7
|
+
* call, so server messages arrive already translated.
|
|
8
|
+
*/
|
|
9
|
+
import i18n from 'i18next';
|
|
10
|
+
import { initReactI18next } from 'react-i18next';
|
|
11
|
+
import { getLocales } from 'expo-localization';
|
|
12
|
+
import en from './locales/en.json';
|
|
13
|
+
import fr from './locales/fr.json';
|
|
14
|
+
|
|
15
|
+
const deviceLanguage = getLocales()[0]?.languageCode || 'en';
|
|
16
|
+
|
|
17
|
+
void i18n.use(initReactI18next).init({
|
|
18
|
+
resources: { en: { translation: en }, fr: { translation: fr } },
|
|
19
|
+
lng: ['en', 'fr'].includes(deviceLanguage) ? deviceLanguage : 'en',
|
|
20
|
+
fallbackLng: 'en',
|
|
21
|
+
interpolation: { escapeValue: false }
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export default i18n;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"app": {
|
|
3
|
+
"name": "Astratra App"
|
|
4
|
+
},
|
|
5
|
+
"auth": {
|
|
6
|
+
"sign_in": "Sign in",
|
|
7
|
+
"email": "Email",
|
|
8
|
+
"password": "Password",
|
|
9
|
+
"forgot": "Forgot your password?",
|
|
10
|
+
"failed": "Sign-in failed. Check your details and try again.",
|
|
11
|
+
"send_link": "Send reset link",
|
|
12
|
+
"reset_sent": "If that address exists, a link is on its way.",
|
|
13
|
+
"show_password": "Show password",
|
|
14
|
+
"hide_password": "Hide password"
|
|
15
|
+
},
|
|
16
|
+
"common": {
|
|
17
|
+
"back": "Back",
|
|
18
|
+
"continue": "Continue",
|
|
19
|
+
"skip": "Skip",
|
|
20
|
+
"step_progress": "Step {{step}} of {{total}}"
|
|
21
|
+
},
|
|
22
|
+
"dashboard": {
|
|
23
|
+
"title": "Dashboard",
|
|
24
|
+
"signed_in_as": "Signed in as",
|
|
25
|
+
"greeting": "Hello",
|
|
26
|
+
"header": {
|
|
27
|
+
"online": "Online",
|
|
28
|
+
"offline": "Offline",
|
|
29
|
+
"subtitle": "Here is where things stand."
|
|
30
|
+
},
|
|
31
|
+
"see_all": "See everything"
|
|
32
|
+
},
|
|
33
|
+
"home": {
|
|
34
|
+
"welcome": "Welcome",
|
|
35
|
+
"start": "Get started"
|
|
36
|
+
},
|
|
37
|
+
"notifications": {
|
|
38
|
+
"title": "Notifications",
|
|
39
|
+
"empty": "Nothing yet.",
|
|
40
|
+
"unread_label": "{{count}} unread notifications"
|
|
41
|
+
},
|
|
42
|
+
"onboarding": {
|
|
43
|
+
"eyebrow": "Setting up",
|
|
44
|
+
"account_title": "Your account",
|
|
45
|
+
"account_help": "We only need an address to start.",
|
|
46
|
+
"done_title": "All set.",
|
|
47
|
+
"done_help": "Your account is ready. Sign in to continue."
|
|
48
|
+
},
|
|
49
|
+
"password": {
|
|
50
|
+
"length": "At least 8 characters",
|
|
51
|
+
"lowercase": "One lowercase letter",
|
|
52
|
+
"uppercase": "One uppercase letter",
|
|
53
|
+
"digit": "One digit",
|
|
54
|
+
"special": "One special character"
|
|
55
|
+
},
|
|
56
|
+
"settings": {
|
|
57
|
+
"title": "Settings",
|
|
58
|
+
"biometric": "Unlock with biometrics",
|
|
59
|
+
"notifications": "Push notifications",
|
|
60
|
+
"sign_out": "Sign out"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"app": {
|
|
3
|
+
"name": "Astratra App"
|
|
4
|
+
},
|
|
5
|
+
"auth": {
|
|
6
|
+
"sign_in": "Se connecter",
|
|
7
|
+
"email": "E-mail",
|
|
8
|
+
"password": "Mot de passe",
|
|
9
|
+
"forgot": "Mot de passe oublié ?",
|
|
10
|
+
"failed": "Connexion impossible. Vérifiez vos informations et réessayez.",
|
|
11
|
+
"send_link": "Envoyer le lien",
|
|
12
|
+
"reset_sent": "Si cette adresse existe, un lien vient de partir.",
|
|
13
|
+
"show_password": "Afficher le mot de passe",
|
|
14
|
+
"hide_password": "Masquer le mot de passe"
|
|
15
|
+
},
|
|
16
|
+
"common": {
|
|
17
|
+
"back": "Retour",
|
|
18
|
+
"continue": "Continuer",
|
|
19
|
+
"skip": "Passer",
|
|
20
|
+
"step_progress": "Étape {{step}} sur {{total}}"
|
|
21
|
+
},
|
|
22
|
+
"dashboard": {
|
|
23
|
+
"title": "Tableau de bord",
|
|
24
|
+
"signed_in_as": "Connecté en tant que",
|
|
25
|
+
"greeting": "Bonjour",
|
|
26
|
+
"header": {
|
|
27
|
+
"online": "En ligne",
|
|
28
|
+
"offline": "Hors ligne",
|
|
29
|
+
"subtitle": "Voici où en sont les choses."
|
|
30
|
+
},
|
|
31
|
+
"see_all": "Tout voir"
|
|
32
|
+
},
|
|
33
|
+
"home": {
|
|
34
|
+
"welcome": "Bienvenue",
|
|
35
|
+
"start": "Commencer"
|
|
36
|
+
},
|
|
37
|
+
"notifications": {
|
|
38
|
+
"title": "Notifications",
|
|
39
|
+
"empty": "Rien pour l'instant.",
|
|
40
|
+
"unread_label": "{{count}} notifications non lues"
|
|
41
|
+
},
|
|
42
|
+
"onboarding": {
|
|
43
|
+
"eyebrow": "Mise en route",
|
|
44
|
+
"account_title": "Votre compte",
|
|
45
|
+
"account_help": "Une adresse suffit pour commencer.",
|
|
46
|
+
"done_title": "C'est prêt.",
|
|
47
|
+
"done_help": "Votre compte est créé. Connectez-vous pour continuer."
|
|
48
|
+
},
|
|
49
|
+
"password": {
|
|
50
|
+
"length": "8 caractères minimum",
|
|
51
|
+
"lowercase": "Une minuscule",
|
|
52
|
+
"uppercase": "Une majuscule",
|
|
53
|
+
"digit": "Un chiffre",
|
|
54
|
+
"special": "Un caractère spécial"
|
|
55
|
+
},
|
|
56
|
+
"settings": {
|
|
57
|
+
"title": "Réglages",
|
|
58
|
+
"biometric": "Déverrouiller par biométrie",
|
|
59
|
+
"notifications": "Notifications",
|
|
60
|
+
"sign_out": "Se déconnecter"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "astratra-mobile-app",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "expo-router/entry",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "expo start",
|
|
7
|
+
"android": "expo run:android",
|
|
8
|
+
"ios": "expo run:ios",
|
|
9
|
+
"test": "node --test features/*.test.mjs"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@astratra/client": "^0.3.0",
|
|
13
|
+
"@astratra/native": "^0.1.0",
|
|
14
|
+
"@shopify/react-native-skia": "2.2.12",
|
|
15
|
+
"expo": "^54.0.0",
|
|
16
|
+
"expo-blur": "~15.0.8",
|
|
17
|
+
"expo-constants": "~18.0.13",
|
|
18
|
+
"expo-crypto": "~15.0.9",
|
|
19
|
+
"expo-device": "~8.0.10",
|
|
20
|
+
"expo-glass-effect": "~0.1.10",
|
|
21
|
+
"expo-linear-gradient": "~15.0.8",
|
|
22
|
+
"expo-linking": "~8.0.12",
|
|
23
|
+
"expo-local-authentication": "~17.0.8",
|
|
24
|
+
"expo-localization": "~17.0.9",
|
|
25
|
+
"expo-notifications": "~0.32.17",
|
|
26
|
+
"expo-router": "~6.0.24",
|
|
27
|
+
"expo-secure-store": "~15.0.8",
|
|
28
|
+
"expo-splash-screen": "~31.0.13",
|
|
29
|
+
"expo-status-bar": "~3.0.9",
|
|
30
|
+
"expo-web-browser": "~15.0.11",
|
|
31
|
+
"i18next": "^26.3.6",
|
|
32
|
+
"react": "19.1.0",
|
|
33
|
+
"react-dom": "19.1.0",
|
|
34
|
+
"react-i18next": "^17.0.11",
|
|
35
|
+
"react-native": "0.81.5",
|
|
36
|
+
"react-native-reanimated": "~4.1.1",
|
|
37
|
+
"react-native-safe-area-context": "~5.6.0",
|
|
38
|
+
"react-native-screens": "~4.16.0",
|
|
39
|
+
"react-native-svg": "15.12.1",
|
|
40
|
+
"react-native-worklets": "0.5.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/react": "~19.1.0",
|
|
44
|
+
"babel-preset-expo": "~54.0.10",
|
|
45
|
+
"typescript": "~5.9.2"
|
|
46
|
+
},
|
|
47
|
+
"private": true
|
|
48
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native notifications, wired to this app.
|
|
3
|
+
*
|
|
4
|
+
* The state machine is in @astratra/native. What belongs here is what the
|
|
5
|
+
* package refuses to guess: which Android channels exist, which action buttons
|
|
6
|
+
* a notification may carry, and which API endpoints register a device.
|
|
7
|
+
*/
|
|
8
|
+
import Constants from 'expo-constants';
|
|
9
|
+
import { Platform } from 'react-native';
|
|
10
|
+
import { router } from 'expo-router';
|
|
11
|
+
import { createPushService } from '@astratra/native';
|
|
12
|
+
import { api, NAMESPACE } from './session';
|
|
13
|
+
import { notificationRouter } from '../features/routes';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* THE NATIVE MODULES ARE LOADED ON DEMAND, NOT AT IMPORT.
|
|
17
|
+
*
|
|
18
|
+
* `expo-notifications` complains loudly the moment it is imported in Expo Go
|
|
19
|
+
* on Android — remote push was removed from it in SDK 53 — and a red error
|
|
20
|
+
* banner greets anyone who merely opens the app, before any notification is
|
|
21
|
+
* involved. Importing here, inside `load()`, means the module is touched only
|
|
22
|
+
* when push is actually used, which in Expo Go is never.
|
|
23
|
+
*
|
|
24
|
+
* This is the whole reason `createPushService` takes an async loader.
|
|
25
|
+
*/
|
|
26
|
+
async function loadNativeModules() {
|
|
27
|
+
const [Notifications, Device, Crypto, SecureStore] = await Promise.all([
|
|
28
|
+
import('expo-notifications'),
|
|
29
|
+
import('expo-device'),
|
|
30
|
+
import('expo-crypto'),
|
|
31
|
+
import('expo-secure-store')
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
notifications: Notifications,
|
|
36
|
+
isDevice: Device.isDevice,
|
|
37
|
+
deviceName: Device.deviceName,
|
|
38
|
+
randomUUID: Crypto.randomUUID,
|
|
39
|
+
keystore: SecureStore
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const push = createPushService({
|
|
44
|
+
namespace: NAMESPACE,
|
|
45
|
+
load: async () => ({
|
|
46
|
+
...(await loadNativeModules()),
|
|
47
|
+
platform: Platform.OS,
|
|
48
|
+
projectId: Constants.easConfig?.projectId
|
|
49
|
+
|| (Constants.expoConfig?.extra as { eas?: { projectId?: string } })?.eas?.projectId,
|
|
50
|
+
/* Android groups notifications by channel, and a channel's importance
|
|
51
|
+
cannot be raised after it is created — declare them deliberately. */
|
|
52
|
+
channels: [{ id: `${NAMESPACE}-general`, name: 'General' }],
|
|
53
|
+
categories: [],
|
|
54
|
+
router: notificationRouter,
|
|
55
|
+
navigate: (route: string) => router.push(route as never),
|
|
56
|
+
api: {
|
|
57
|
+
register: (registration) => api.request('/notifications/devices', {
|
|
58
|
+
method: 'POST',
|
|
59
|
+
body: JSON.stringify(registration)
|
|
60
|
+
}),
|
|
61
|
+
current: (installationId) => api.request(`/notifications/devices/${installationId}`),
|
|
62
|
+
unregister: (installationId) => api.request(`/notifications/devices/${installationId}`, {
|
|
63
|
+
method: 'DELETE'
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session, and the API that carries it.
|
|
3
|
+
*
|
|
4
|
+
* Everything hard here lives in @astratra/native and @astratra/client: the
|
|
5
|
+
* keystore contract, the single-flight refresh, the replay-once rule. This
|
|
6
|
+
* file only says WHICH keystore, WHICH base URL, and what to do when a session
|
|
7
|
+
* finally expires.
|
|
8
|
+
*/
|
|
9
|
+
import { Platform } from 'react-native';
|
|
10
|
+
import * as SecureStore from 'expo-secure-store';
|
|
11
|
+
import Constants from 'expo-constants';
|
|
12
|
+
import { createApiClient, createSecureSession, createWebKeystore } from '@astratra/native';
|
|
13
|
+
import i18n from '../i18n/config';
|
|
14
|
+
|
|
15
|
+
/** Change this once, here. Everything else reads it from the session module. */
|
|
16
|
+
export const NAMESPACE = 'astratra';
|
|
17
|
+
|
|
18
|
+
/* The Android emulator lives in its own network VM: "localhost" there means
|
|
19
|
+
the emulator itself, not the machine running the API. 10.0.2.2 is the
|
|
20
|
+
standard alias back to the host. The iOS simulator shares the Mac's network
|
|
21
|
+
and needs no such trick. */
|
|
22
|
+
const DEV_HOST = Platform.OS === 'android' ? '10.0.2.2' : 'localhost';
|
|
23
|
+
|
|
24
|
+
/* No `/api` suffix: the API generated by `--template api|fullstack` mounts its
|
|
25
|
+
routes at the root (`/auth/login`, `/users`, …). Point this at whatever your
|
|
26
|
+
server actually serves — EXPO_PUBLIC_API_BASE_URL wins over the default. */
|
|
27
|
+
export const API_BASE_URL = String(
|
|
28
|
+
Constants.expoConfig?.extra?.apiBaseUrl
|
|
29
|
+
|| process.env.EXPO_PUBLIC_API_BASE_URL
|
|
30
|
+
|| `http://${DEV_HOST}:3000`
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
/* expo-secure-store has no real web implementation. localStorage is NOT a
|
|
34
|
+
keystore — it is here so a browser preview can be clicked through in
|
|
35
|
+
development, never to hold a session in a shipped build. */
|
|
36
|
+
const keystore = Platform.OS === 'web'
|
|
37
|
+
? createWebKeystore(typeof localStorage === 'undefined' ? null : localStorage)
|
|
38
|
+
: SecureStore;
|
|
39
|
+
|
|
40
|
+
export const session = createSecureSession({ keystore, namespace: NAMESPACE });
|
|
41
|
+
|
|
42
|
+
let onExpired: () => void = () => {};
|
|
43
|
+
|
|
44
|
+
/** Called by the auth context once it knows how to route to the sign-in screen. */
|
|
45
|
+
export function onSessionExpired(handler: () => void) {
|
|
46
|
+
onExpired = handler;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function refresh() {
|
|
50
|
+
const refreshToken = await session.getRefreshToken();
|
|
51
|
+
if (!refreshToken) throw new Error('NO_REFRESH_TOKEN');
|
|
52
|
+
|
|
53
|
+
const response = await fetch(`${API_BASE_URL}/auth/refresh`, {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
headers: { 'Content-Type': 'application/json' },
|
|
56
|
+
body: JSON.stringify({ refreshToken })
|
|
57
|
+
});
|
|
58
|
+
if (!response.ok) throw new Error('REFRESH_FAILED');
|
|
59
|
+
|
|
60
|
+
/* The server names the access token `token`, not `accessToken`. Reading the
|
|
61
|
+
wrong field fails silently: the session is "saved" with undefined, and the
|
|
62
|
+
next request goes out unauthenticated. */
|
|
63
|
+
const payload = await response.json();
|
|
64
|
+
await session.save({
|
|
65
|
+
accessToken: payload?.data?.token,
|
|
66
|
+
refreshToken: payload?.data?.refreshToken
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const api = createApiClient({
|
|
71
|
+
baseUrl: API_BASE_URL,
|
|
72
|
+
session,
|
|
73
|
+
refresh,
|
|
74
|
+
/* Read per call, never cached: the person changes language without a restart. */
|
|
75
|
+
language: () => String(i18n.language || '').slice(0, 2) || null,
|
|
76
|
+
onSessionExpired: () => onExpired()
|
|
77
|
+
});
|