-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
241 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
# This is login page | ||
|
||
## `signin(email, password, res)` Method | ||
|
||
The `signin` method is used to authenticate a user by verifying their email and password. If the provided email and password match a user's credentials in the database, this method returns a user object, a JSON Web Token (JWT) for user authentication, and their role. In case of invalid email, password, or other issues, this method throws a `HajarError` to handle the error gracefully. | ||
|
||
### Method Signature | ||
|
||
```javascript | ||
async signin(email, password, res) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `email` (String): The email address of the user attempting to sign in. | ||
- `password` (String): The password associated with the user's email. | ||
- `res` (Object, optional): The response object to set a cookie for the authenticated user. This parameter is optional and can be omitted if you do not want to set cookies. | ||
|
||
### Return Value | ||
|
||
This method returns an object containing the following properties: | ||
|
||
- `user` (Object): The user object retrieved from the database. | ||
- `token` (String): A JSON Web Token (JWT) for user authentication. | ||
- `role` (String): The role of the authenticated user. | ||
|
||
### Error Handling | ||
|
||
If the provided email or password is invalid or if there are other issues during the authentication process, this method throws a `HajarError`. The error object includes the following properties: | ||
|
||
- `message` (String): A descriptive error message. | ||
- `code` (String): A unique error code to identify the type of error. | ||
- `data` (Object, optional): Additional data related to the error. | ||
|
||
### Example Usage | ||
|
||
```javascript | ||
try { | ||
const email = "[email protected]"; | ||
const password = "securePassword"; | ||
const res = {}; // Optional response object for setting cookies | ||
const authResult = await hajarAuth.signin(email, password, res); | ||
console.log("Authentication successful:", authResult); | ||
} catch (error) { | ||
console.error("Authentication failed:", error); | ||
} | ||
``` | ||
|
||
### Note | ||
|
||
- Make sure to securely store the JWT token provided by this method for subsequent authenticated requests. | ||
- The `res` parameter is optional and is typically used when setting cookies for user sessions. If not needed, you can omit it. | ||
|
||
--- | ||
|
||
That concludes the documentation for the `signin` method of the HajarAuth class. You can use this method to authenticate users in your SaaS application securely. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
# this is register page | ||
|
||
## `signup(username, email, password)` | ||
|
||
The `signup` method allows you to register a new user in your SaaS application. It takes three parameters: `username`, `email`, and `password`. Upon successful registration, it returns a response object containing user information and a JWT token for authentication. | ||
|
||
### Parameters | ||
|
||
- `username` (String): The desired username for the new user. | ||
- `email` (String): The user's email address. It must be unique and not already registered. | ||
- `password` (String): The user's password. It should be securely hashed before calling this method. | ||
|
||
### Returns | ||
|
||
A successful registration returns an object with the following properties: | ||
|
||
- `success` (Boolean): Indicates whether the registration was successful. | ||
- `user` (Object): An object containing user information, including: | ||
- `_id` (String): The unique ID of the registered user. | ||
- `id` (String): An alias for the user's ID. | ||
- `email` (String): The email address of the registered user. | ||
- `uid` (String): A unique identifier for the user. | ||
- `firstName` (Object): An object containing the user's first name in different languages. | ||
- `lastName` (Object): An object containing the user's last name in different languages. | ||
- `role` (String): The role assigned to the user. | ||
|
||
- `token` (String): A JSON Web Token (JWT) that can be used for user authentication. | ||
|
||
### Errors | ||
|
||
This method may throw errors in the following situations: | ||
|
||
- If a user with the provided email already exists: | ||
- Error message: "User with this email already exists" | ||
- Error code: "user-already-exist" | ||
- Additional information: `{ email: email }` | ||
|
||
- If the "Admin" role is not found: | ||
- Error message: "Admin role not found" | ||
- Error code: "admin-role-not-found" | ||
|
||
- If an error occurs while creating the user or admin profile: | ||
- The error details will be logged, and the error will be rethrown. | ||
|
||
### Example | ||
|
||
```javascript | ||
try { | ||
const registrationResult = await hajarAuth.signup("john_doe", "[email protected]", "securePassword"); | ||
|
||
if (registrationResult.success) { | ||
console.log("User registered successfully:", registrationResult.user); | ||
console.log("JWT Token:", registrationResult.token); | ||
} else { | ||
console.error("Registration failed."); | ||
} | ||
} catch (error) { | ||
console.error("Error during registration:", error.message); | ||
} | ||
``` | ||
|
||
In this example, the `signup` method is used to register a new user with the provided username, email, and password. If the registration is successful, the user's information and JWT token are logged. If an error occurs, it is caught and logged. | ||
|
||
--- | ||
|
||
That's the complete documentation for the `signup` method of the `HajarAuth` class. You can use this method to facilitate user registration in your SaaS application and manage the associated errors effectively. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"setup": "Setup Database", | ||
"setupDatabase": "Setup Database", | ||
"connect": "Connect Database" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This is connect database |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"setupMongoDB": "MongoDB", | ||
"setupFirebaseDB": "Firebase DB", | ||
"setupMySQL": "MySQL", | ||
"setupPostgress": "PostgreSQL", | ||
"setupSQL": "SQL" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
<Callout type="info"> | ||
This function will be added in the future inshallah. | ||
</Callout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## setupMongoDB(`options`, `middleware`) | ||
|
||
### Parameters | ||
|
||
- `options` (Object): Configuration options for MongoDB. These options typically include a database URL and any additional settings required for the connection. | ||
- `middleware` (Object): The MongoDB middleware or library used for connecting to MongoDB. This can be a library like Mongoose or the native MongoDB driver. | ||
|
||
### Returns | ||
|
||
The `setupMongoDB` function returns a MongoDB connection object. This object represents the connection to the MongoDB database and allows you to interact with the database. | ||
|
||
The MongoDB connection object typically has the following properties and methods: | ||
|
||
- `readyState` (Boolean): Indicates the current state of the connection (e.g., connected, disconnected, connecting). | ||
- `close` (Function): A method that can be used to close the MongoDB connection when it is no longer needed. | ||
|
||
### Errors | ||
|
||
The `setupMongoDB` function may throw an error if an issue occurs during the MongoDB connection setup. In such cases, an `Error` object is thrown with a descriptive error message. | ||
|
||
### Example | ||
|
||
```javascript | ||
const mongoose = require("mongoose"); | ||
|
||
// Configuration options for MongoDB | ||
const mongoDBOptions = { | ||
url: "mongodb://localhost/mydatabase", | ||
}; | ||
|
||
try { | ||
// Set up MongoDB connection using Mongoose middleware | ||
const mongoDBConnection = setupMongoDB(mongoDBOptions, mongoose); | ||
|
||
// Check the connection state | ||
if (mongoDBConnection.readyState === mongoose.connection.readyState) { | ||
console.log("MongoDB connected successfully."); | ||
} else { | ||
console.error("MongoDB connection failed."); | ||
} | ||
} catch (error) { | ||
console.error("Error setting up MongoDB connection:", error.message); | ||
} | ||
``` | ||
|
||
In this example, the `setupMongoDB` function is used to set up and configure a MongoDB connection using the Mongoose middleware. It checks the connection state and logs whether the MongoDB connection was successful or encountered an error. | ||
|
||
--- | ||
|
||
That's the complete documentation for the `setupMongoDB` function, providing details about its purpose, parameters, return value, potential errors, and a usage example. This documentation helps users understand how to set up a MongoDB connection in your application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
<Callout type="info"> | ||
This function will be added in the future inshallah. | ||
</Callout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
<Callout type="info"> | ||
This function will be added in the future inshallah. | ||
</Callout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
<Callout type="info"> | ||
This function will be added in the future inshallah. | ||
</Callout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@sikka/hajar", | ||
"version": "1.1.10", | ||
"version": "1.1.11", | ||
"description": "Toolkit to create SaaS applications", | ||
"author": "Sikka Software <[email protected]> (http://sikka.io)", | ||
"license": "MIT", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters