@airsoko/auth 0.0.1 → 0.0.3

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.
Files changed (2) hide show
  1. package/README.md +236 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,236 @@
1
+ # Airsoko auth package
2
+
3
+ ## 📑 Table of Contents
4
+
5
+ <details>
6
+ <summary>Table</summary>
7
+
8
+ - [Overview](#overview)
9
+ - [Installation ](#installation)
10
+ - [Prerequisites](#prerequisites)
11
+ - [Installation](#global-installation)
12
+ - [Using Airsoko ](#Project-Installation)
13
+ - [Updating](#updating)
14
+ - [Uninstallation](#uninstalling)
15
+ - [API Reference](#api-reference)
16
+ - [License](#license)
17
+ - [Built With](#built-with)
18
+
19
+ </details>
20
+
21
+ ## Overview
22
+
23
+ The npm package provides essential utilities for handling authentication within the Airsoko application.
24
+
25
+ ## Installation
26
+
27
+ ```bash
28
+ npm install @airsoko/auth
29
+ ```
30
+
31
+ ### Prerequisites
32
+
33
+ - Node.js (version 16 or higher)
34
+ - npm (or yarn)
35
+
36
+ ### Uninstalling
37
+
38
+ If you wish to uninstall Airsoko next, use:
39
+
40
+ ```sh
41
+ npm uninstall @airsoko/auth
42
+ ```
43
+
44
+ ### API Reference
45
+
46
+ ## Example
47
+
48
+ ### Step 1: Wrap the Application with `AuthProvider`
49
+
50
+ To enable authentication within your React application, you need to wrap it with the `AuthProvider` component provided by the Macive Auth package. This component sets up the necessary authentication context, allowing your components to access authentication-related information.
51
+
52
+ Here's an example of how to use the `AuthProvider`:
53
+
54
+ ```typescript
55
+ import { AuthProvider } from "@airsoko/auth;
56
+
57
+ <AuthProvider
58
+ domain='my-shop.airsoko.com'
59
+ clientId="YOUR_AUTH_CLIENT_ID"
60
+ redirectUri='https://my-shop.airsoko.com'
61
+ audience="CUSTOMER"
62
+ >
63
+ {/* Your application components */}
64
+ </AuthProvider>
65
+ ```
66
+
67
+ Parameters:
68
+
69
+ - domain: The Airsoko domain associated with your application. .
70
+
71
+ - clientId: Your Airsoko client ID. Replace "YOUR_AUTH0_CLIENT_ID" with the actual client ID for your application.
72
+
73
+ - redirectUri: The URI to which the user will be redirected after authentication. It is usually set to the application's domain or https://my-shop.airsoko.com/my-auth-dwstination'.
74
+
75
+ - audience: The audience parameter for Auth0, typically representing the target audience for authentication. In this example, it is set to "CUSTOMER".
76
+
77
+ ### Step 2: Authentication Hook use with `useAuth`
78
+
79
+ The useAuth hook provides a convenient way to integrate authentication functionalities into your React application. This hook allows you to access user information, check authentication status, and perform actions based on the authentication state.
80
+
81
+ ### Usage Example: Private Route Component
82
+
83
+ Here's an example of how you can use the useAuth hook to create a private route component using Next.js:
84
+
85
+ ```typescript
86
+ import { useAuth } from "@airsoko/auth";
87
+ import Link from "next/link";
88
+ import React from "react";
89
+
90
+ type AuthPropsType = {
91
+ permissions?: string[];
92
+ };
93
+
94
+ interface PrivateRouteProps {
95
+ authProps: AuthPropsType;
96
+ children: React.ReactNode;
97
+ }
98
+
99
+ const PrivateRoute: React.FC<PrivateRouteProps> = ({ children, authProps }) => {
100
+ const { user, goLogin, sessionLoading, isAuthenticated } = useAuth();
101
+ const isUser = user?.id;
102
+
103
+ React.useEffect(() => {
104
+ if (!sessionLoading) {
105
+ if (!isAuthenticated) {
106
+ if (!isUser) goLogin();
107
+ } else {
108
+ // Additional logic for authenticated users, if needed.
109
+ }
110
+ }
111
+ }, [isUser, sessionLoading]);
112
+
113
+ // If the user is authenticated and has the required permissions.
114
+ // Replace with your own permission checking logic if needed.
115
+ if (isUser) {
116
+ // Render the protected content.
117
+ return <>{children}</>;
118
+ }
119
+
120
+ // Session is being fetched, or no user.
121
+ // If no user, useEffect() will redirect to the login page.
122
+ return <GlobalLoader />;
123
+ };
124
+
125
+ ```
126
+
127
+ ## Functions
128
+
129
+ ### 1. `goLogin` Function - Redirect to Login
130
+
131
+ The goLogin function is designed to redirect users to the login page. This function can be useful in scenarios where you want to ensure users are authenticated before accessing certain parts of your application.
132
+
133
+ ```typescript
134
+ import { useAuth } from "@airsoko/auth";
135
+
136
+ const YourComponent: React.FC = () => {
137
+ const { goLogin } = useAuth();
138
+
139
+ const handleLoginButtonClick = () => {
140
+ goLogin();
141
+ };
142
+
143
+ return (
144
+ <div>
145
+ <p>Welcome to your application!</p>
146
+ <button onClick={handleLoginButtonClick}>Login</button>
147
+ </div>
148
+ );
149
+ };
150
+
151
+ ```
152
+
153
+ ### 2. `signIn` Function - Sign In and Redirect
154
+
155
+ The `signIn` function is designed to sign in the user to the application using a provided token and redirect them to a specific destination. This function is useful when you want to handle custom authentication logic and redirect users after successful sign-in.
156
+
157
+ ```typescript
158
+ import { useAuth } from "@airsoko/auth";
159
+
160
+ const YourComponent: React.FC = () => {
161
+ const { signIn } = useAuth();
162
+
163
+ const handleSignInButtonClick = async () => {
164
+ try {
165
+ // Perform your authentication logic to obtain a token.
166
+ const authToken = "YOUR_OBTAINED_TOKEN";
167
+
168
+ // Use the signIn function to sign in the user and redirect.
169
+ await signIn(authToken);
170
+
171
+ // After successful sign-in, the user will be redirected.
172
+ } catch (error) {
173
+ console.error("Authentication failed:", error);
174
+ // Handle authentication failure.
175
+ }
176
+ };
177
+
178
+ return (
179
+ <div>
180
+ <p>Welcome to your application!</p>
181
+ <button onClick={handleSignInButtonClick}>Sign In</button>
182
+ </div>
183
+ );
184
+ };
185
+
186
+ ```
187
+
188
+ ### 3. `logout` Function - Log Out User
189
+
190
+ The logout function is designed to log out the user from the application. This function can be used to initiate the logout process, which may include clearing session data and redirecting the user to a specified destination.
191
+
192
+ ```typescript
193
+ import { useAuth } from "@airsoko/auth";
194
+
195
+ const YourComponent: React.FC = () => {
196
+ const { logout } = useAuth();
197
+
198
+ const handleLogoutButtonClick = async () => {
199
+ try {
200
+ // Use the logout function to log out the user.
201
+ await logout();
202
+
203
+ // After successful logout, you can perform additional actions or redirect the user.
204
+ console.log("User successfully logged out.");
205
+ } catch (error) {
206
+ console.error("Logout failed:", error);
207
+ // Handle logout failure.
208
+ }
209
+ };
210
+
211
+ return (
212
+ <div>
213
+ <p>Welcome to your application!</p>
214
+ <button onClick={handleLogoutButtonClick}>Logout</button>
215
+ </div>
216
+ );
217
+ };
218
+
219
+ ```
220
+
221
+ ## 🤝 Contributing
222
+
223
+ Contributions to improve this package are welcome. Please adhere to the project's coding standards and commit guidelines.
224
+
225
+ ## License
226
+
227
+ MIT License
228
+
229
+ ## ⚒️ Built With
230
+
231
+ - ![@types/node](https://img.shields.io/badge/@types/node-40A2D8?style=for-the-badge&logo=%40types%2Fnode&logoColor=white)
232
+ - ![typescript](https://img.shields.io/badge/typescript-40A2D8?style=for-the-badge&logo=typescript&logoColor=white)
233
+
234
+ ***
235
+
236
+ _🌟 This README was generated with 💖 by [Airsoko](https://github.com/airsoko)_
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@airsoko/auth",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,