Skip to content

Commit

Permalink
Added more docs pages
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaakher committed Oct 5, 2023
1 parent a45fce4 commit 2d0d574
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 11 deletions.
56 changes: 55 additions & 1 deletion docs/pages/docs/authentication/login.mdx
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.
66 changes: 65 additions & 1 deletion docs/pages/docs/authentication/register.mdx
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.
2 changes: 1 addition & 1 deletion docs/pages/docs/database/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"setup": "Setup Database",
"setupDatabase": "Setup Database",
"connect": "Connect Database"
}
1 change: 1 addition & 0 deletions docs/pages/docs/database/connect.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is connect database
1 change: 0 additions & 1 deletion docs/pages/docs/database/setup.mdx

This file was deleted.

7 changes: 7 additions & 0 deletions docs/pages/docs/database/setupDatabase/_meta.json
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"
}
5 changes: 5 additions & 0 deletions docs/pages/docs/database/setupDatabase/setupFirebaseDB.mdx
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>
50 changes: 50 additions & 0 deletions docs/pages/docs/database/setupDatabase/setupMongoDB.mdx
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.
5 changes: 5 additions & 0 deletions docs/pages/docs/database/setupDatabase/setupMySQL.mdx
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>
5 changes: 5 additions & 0 deletions docs/pages/docs/database/setupDatabase/setupPostgress.mdx
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>
5 changes: 5 additions & 0 deletions docs/pages/docs/database/setupDatabase/setupSQL.mdx
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>
2 changes: 1 addition & 1 deletion package.json
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",
Expand Down
47 changes: 41 additions & 6 deletions src/core/database/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Set up and configure a database connection based on the specified type.
*
* @param {string} type - The type of database (e.g., "MongoDB", "SQL", "MySQL").
* @param {object} options - Configuration options specific to the chosen database.
* @param {object} middleware - Middleware or library for connecting to the database (e.g., Mongoose for MongoDB).
* @returns {object|null} - A database connection object or `null` if the type is not recognized.
*/
function setupDatabase(type, options, middleware) {
switch (type) {
case "MongoDB":
Expand All @@ -9,36 +17,63 @@ function setupDatabase(type, options, middleware) {
case "Supabase":
return setupSupabase(options);
case "Firebase Database":
return setupFirebaseDatabase(options);
return setupFirebaseDB(options);
default:
return null;
}
}

/**
* Set up and configure a MongoDB connection.
*
* @param {object} options - Configuration options for MongoDB (e.g., URL, options).
* @param {object} middleware - The MongoDB middleware or library (e.g., Mongoose).
* @returns {object} - A MongoDB connection object.
*/
function setupMongoDB(options, middleware) {
const connection = middleware.connect(options.url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
return connection;
}

/**
* Set up and configure an SQL database connection.
*
* @param {object} options - Configuration options for the SQL database.
* @returns {object} - An SQL database connection object.
*/
function setupSQL(options) {
// Connect to SQL using the options provided
// ...
}

/**
* Set up and configure a MySQL database connection.
*
* @param {object} options - Configuration options for the MySQL database.
* @returns {object} - A MySQL database connection object.
*/
function setupMySQL(options) {
// Connect to MySQL using the options provided
// ...
}

/**
* Set up and configure a Supabase database connection.
*
* @param {object} options - Configuration options for Supabase.
* @returns {object} - A Supabase database connection object.
*/
function setupSupabase(options) {
// Connect to Supabase using the options provided
// ...
}

function setupFirebaseDatabase(options) {
/**
* Set up and configure a Firebase Database connection.
*
* @param {object} options - Configuration options for Firebase Database.
* @returns {object} - A Firebase Database connection object.
*/
function setupFirebaseDB(options) {
// Connect to Firebase Database using the options provided
// ...
}
Expand Down

0 comments on commit 2d0d574

Please sign in to comment.