reactjs-cognito-auth-module 0.0.0-stage → 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.
@@ -0,0 +1,6 @@
1
+ import React from "react";
2
+ export interface AuthenticatorProps {
3
+ coucou: string;
4
+ }
5
+ declare const Authenticator: (props: AuthenticatorProps) => React.JSX.Element;
6
+ export default Authenticator;
@@ -0,0 +1 @@
1
+ export { default } from "./Authenticator";
@@ -0,0 +1,6 @@
1
+ import type { AuthModuleConfig } from "./types";
2
+ /**
3
+ * Amplify configuration
4
+ * Called once at service construction
5
+ */
6
+ export declare function configureAmplify(config: AuthModuleConfig): void;
@@ -0,0 +1,21 @@
1
+ import { type ReactNode } from "react";
2
+ import type { AuthModuleConfig, AuthResult, AuthStatus, AuthUser, PasswordlessChannel, SocialProvider } from "./types";
3
+ interface AuthContextValue {
4
+ status: AuthStatus;
5
+ user: AuthUser | null;
6
+ isAuthenticated: boolean;
7
+ socialProviders: SocialProvider[];
8
+ signInWithSocial: (provider: SocialProvider) => Promise<AuthResult>;
9
+ signIn: (username: string, password: string) => Promise<AuthResult>;
10
+ initiatePasswordlessSignIn: (identifier: string, channel?: PasswordlessChannel) => Promise<AuthResult>;
11
+ confirmPasswordlessSignIn: (identifier: string, code: string) => Promise<AuthResult<AuthUser>>;
12
+ signOut: () => Promise<AuthResult>;
13
+ refreshSession: () => Promise<AuthResult>;
14
+ }
15
+ interface AuthProviderProps {
16
+ config: AuthModuleConfig;
17
+ children: ReactNode;
18
+ }
19
+ export declare function AuthProvider({ config, children }: AuthProviderProps): import("react").JSX.Element;
20
+ export declare function useAuthContext(): AuthContextValue;
21
+ export {};
@@ -0,0 +1,18 @@
1
+ import { JSX } from "react";
2
+ export interface APInterface {
3
+ isAuthenticated(): boolean;
4
+ getUser(): string | null;
5
+ signIn(email: string, password: string, setAuthenticated: (b: boolean) => void, setError: (err: string[]) => void): Promise<void>;
6
+ signOut(): void;
7
+ }
8
+ export interface UIInterface {
9
+ signInPage(): JSX.Element;
10
+ }
11
+ export interface AuthenticatorProps {
12
+ children: JSX.Element;
13
+ mustBeAuthenticated: boolean;
14
+ ui: UIInterface;
15
+ auth: APInterface;
16
+ }
17
+ declare const Authenticator: (props: AuthenticatorProps) => JSX.Element;
18
+ export default Authenticator;
@@ -0,0 +1,61 @@
1
+ import type { AuthListeners, AuthModuleConfig, AuthResult, AuthStatus, AuthUser, PasswordlessChannel, SocialProvider } from "./types";
2
+ /**
3
+ * Authentication service
4
+ */
5
+ export declare class CognitoAuthService {
6
+ private config;
7
+ private listeners;
8
+ private _status;
9
+ private _user;
10
+ private refreshTimer;
11
+ private unsubscribeAuthChange;
12
+ constructor(config: AuthModuleConfig);
13
+ get status(): AuthStatus;
14
+ get user(): AuthUser | null;
15
+ get isAuthenticated(): boolean;
16
+ /**
17
+ * Initialisation: check existing session, subscribe to notification and start refresh process.
18
+ */
19
+ initialize(): Promise<AuthResult<AuthUser>>;
20
+ /**
21
+ * Destructor.
22
+ */
23
+ destroy(): void;
24
+ /**
25
+ * Cognito signin flow
26
+ */
27
+ signIn(username: string, password: string): Promise<AuthResult>;
28
+ /**
29
+ * Step1: sent verification code by mail or SMS
30
+ *
31
+ * Amplify v6 : `signIn({ username })` avec un User Pool configuré
32
+ * en "Email or phone number" (sans password) déclenche automatiquement
33
+ * l'envoi du code et retourne un challenge `CONFIRM_SIGN_IN_WITH_CODE`.
34
+ */
35
+ initiatePasswordlessSignIn(identifier: string, _channel?: PasswordlessChannel): Promise<AuthResult>;
36
+ /**
37
+ * Step 2 : authentication confirmation with the code received by email or SMS.
38
+ */
39
+ confirmPasswordlessSignIn(_identifier: string, code: string): Promise<AuthResult<AuthUser>>;
40
+ /**
41
+ * Start OAuth flow with social provider
42
+ *
43
+ * Amplify v6 : `signInWithProvider({ provider })` redirige vers
44
+ * la page de consentement. Au retour, Amplify échange le code
45
+ * automatiquement. On vérifie ensuite la session.
46
+ */
47
+ signInWithSocial(provider: SocialProvider): Promise<AuthResult>;
48
+ signOut(): Promise<AuthResult>;
49
+ refreshSession(): Promise<AuthResult>;
50
+ requestPasswordReset(identifier: string): Promise<AuthResult>;
51
+ confirmPasswordReset(identifier: string, code: string, newPassword: string): Promise<AuthResult>;
52
+ setListeners(listeners: AuthListeners): void;
53
+ /** Get the current user into a private AuthUser */
54
+ private fetchCurrentUser;
55
+ private mapToAuthUser;
56
+ private setUser;
57
+ private setStatus;
58
+ private startSessionRefresh;
59
+ private stopSessionRefresh;
60
+ private normalizeError;
61
+ }
@@ -0,0 +1,3 @@
1
+ export { AuthProvider } from "./AuthContext";
2
+ export { useAuth } from "./useAuth";
3
+ export type { AuthModuleConfig, AuthResult, AuthStatus, AuthUser, AuthError, SocialProvider, PasswordlessChannel, } from "./types";
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Identifier of Cognito social provider.
3
+ * Must correspond to identifier configured in the User Pool.
4
+ */
5
+ export type SocialProvider = "Google" | "Facebook" | "Apple" | "Amazon";
6
+ /**
7
+ * Passwordfree authentication channels.
8
+ */
9
+ export type PasswordlessChannel = "EMAIL" | "SMS";
10
+ /**
11
+ * Status of authentication.
12
+ */
13
+ export type AuthStatus = "idle" | "loading" | "authenticated" | "unauthenticated" | "error";
14
+ /**
15
+ * Authenticated user.
16
+ */
17
+ export interface AuthUser {
18
+ userId: string;
19
+ username: string;
20
+ email?: string;
21
+ phone?: string;
22
+ attributes?: Record<string, string>;
23
+ /** Is authenticated by social media, the used provider. */
24
+ socialProvider?: SocialProvider;
25
+ accessToken?: any;
26
+ idToken?: any;
27
+ }
28
+ /**
29
+ * Generic result of authentication.
30
+ */
31
+ export interface AuthResult<T = void> {
32
+ success: boolean;
33
+ data?: T;
34
+ error?: AuthError;
35
+ }
36
+ /**
37
+ * Authentication error.
38
+ */
39
+ export interface AuthError {
40
+ code: string;
41
+ message: string;
42
+ cause?: unknown;
43
+ }
44
+ /**
45
+ * Authentification Cognito Configuration.
46
+ */
47
+ export interface AuthModuleConfig {
48
+ region: string;
49
+ userPoolId: string;
50
+ userPoolWebClientId: string;
51
+ /** OAuth configured in the User Pool. */
52
+ socialProviders: SocialProvider[];
53
+ /** Default channel for passwordfree authentication. */
54
+ defaultPasswordlessChannel?: PasswordlessChannel;
55
+ /** Session time to live (ms). Triggers the automatic refresh. */
56
+ sessionRefreshIntervalMs?: number;
57
+ }
58
+ /**
59
+ * React context notifiation callbacks.
60
+ */
61
+ export interface AuthListeners {
62
+ onAuthChange?: (status: AuthStatus, user?: AuthUser) => void;
63
+ onError?: (error: AuthError) => void;
64
+ }
@@ -0,0 +1,20 @@
1
+ import type { AuthResult, PasswordlessChannel, SocialProvider } from "./types";
2
+ /**
3
+ * Hook to consume authentification in a React component.
4
+ * @example
5
+ * const { user, isAuthenticated, signInWithSocial, pending, error } = useAuth();
6
+ */
7
+ export declare function useAuth(): {
8
+ pending: boolean;
9
+ error: string | null;
10
+ signIn: (username: string, password: string) => Promise<AuthResult<void>>;
11
+ signInWithSocial: (provider: SocialProvider) => Promise<AuthResult<void>>;
12
+ initiatePasswordlessSignIn: (identifier: string, channel?: PasswordlessChannel) => Promise<AuthResult<void>>;
13
+ confirmPasswordlessSignIn: (identifier: string, code: string) => Promise<AuthResult<import("./types").AuthUser>>;
14
+ signOut: () => Promise<AuthResult<void>>;
15
+ status: import("./types").AuthStatus;
16
+ user: import("./types").AuthUser | null;
17
+ isAuthenticated: boolean;
18
+ socialProviders: SocialProvider[];
19
+ refreshSession: () => Promise<AuthResult>;
20
+ };
package/package.json CHANGED
@@ -1,6 +1,43 @@
1
1
  {
2
2
  "name": "reactjs-cognito-auth-module",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "private": false,
4
+ "version": "0.1.0",
5
+ "type": "module",
6
+ "description": "reactjs authentication module based on Cognito",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/lucre74/reactjs-configurable-auth-module.git"
10
+ },
11
+ "main": "dist/cjs/index.js",
12
+ "module": "dist/esm/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "rollup -c",
19
+ "test": "echo \"Error: no test specified\" && exit 1"
20
+ },
21
+ "author": "Luc Lejoly",
22
+ "license": "BSD-3-Clause",
23
+ "peerDependencies": {
24
+ "react": "^17.0.0 || ^18.0.0",
25
+ "react-dom": "^17.0.0 || ^18.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "@babel/preset-react": "^7.29.7",
29
+ "@babel/preset-typescript": "^7.29.7",
30
+ "@rollup/plugin-commonjs": "^29.0.3",
31
+ "@rollup/plugin-node-resolve": "^16.0.3",
32
+ "@rollup/plugin-typescript": "^12.3.0",
33
+ "@types/react": "^19.3.0",
34
+ "@types/react-dom": "^19.3.0",
35
+ "aws-amplify": "^6.22.2",
36
+ "react": "^19.3.1",
37
+ "react-dom": "^19.3.1",
38
+ "rollup": "^4.64.8",
39
+ "rollup-plugin-dts": "^6.5.1",
40
+ "tslib": "^2.8.1",
41
+ "typescript": "~5.7.0"
42
+ }
43
+ }