@airsoko/auth 0.0.1 → 0.0.2

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