# estol-auth

> Autnentication & Authorization, passport based dependency

Latest version **1.0.2** (published 2023-10-31) · AGPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install estol-auth
pnpm add estol-auth
yarn add estol-auth
bun add estol-auth
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-10-31 |
| First published | 2023-10-31 |
| Weekly downloads | 0 |
| License | AGPL-3.0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 49.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Esteban Guillermo Olivera Pereyra |
| Maintainers | estol1991 |
| Keywords | auth, oauth, authorization, passport |

## Links

- npm: https://www.npmjs.com/package/estol-auth
- Repository: https://github.com/estol-web-design/dependency-auth
- Homepage: https://github.com/estol-web-design/dependency-auth#readme
- Issues: https://github.com/estol-web-design/dependency-auth/issues
- npm.io page: https://npm.io/package/estol-auth

## Dependencies (8)

- [bcryptjs](https://npm.io/package/bcryptjs.md) ^2.4.3
- [passport](https://npm.io/package/passport.md) ^0.6.0
- [passport-local](https://npm.io/package/passport-local.md) ^1.0.0
- [passport-github2](https://npm.io/package/passport-github2.md) ^0.1.12
- [passport-twitter](https://npm.io/package/passport-twitter.md) ^1.0.4
- [passport-facebook](https://npm.io/package/passport-facebook.md) ^3.0.0
- [passport-microsoft](https://npm.io/package/passport-microsoft.md) ^1.0.0
- [passport-google-oauth20](https://npm.io/package/passport-google-oauth20.md) ^2.0.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-10-31
- 1.0.1 — 2023-10-31

## README

# estol-auth

This module provides authorization and authentication functionalities for a Node.js application. It includes local authentication and valious OAuth strategies (Facebook, GitHub, Google, Microsoft, Twitter). The module uses bcrypt for password and provides utility functions for password verification and hashing.

## Instalation

```
$ npm install git+https://github.com/estol-web-design/dependency-auth.git
```

## Usage

### User model

You must define the user model that meets all your requirements for the app you are developing. This dependency only requires that this model contains three fields: first one named 'email' that should have 'required' and 'unique' properties, a second one named 'password', and a third one named 'service' that should have a 'required' property.

```javascript
const userSchema = new Schema({
   email: { type: String, unique: true, required: true },
   password: { type: String },
   service: { type: String, required: true },
});
```

### Importing the module in your auth managment file

Import in your auth managment file the auth strategies that you need this way

```javascript
import strategies from "estol-auth";

// here you can define constants for the strategies you need
const { local, facebook, github, google, microsoft, twitter } = strategies;
```

To implement this strategies in your project you will need to install passport in your project.

#### Example

```javascript
import passport from "passport";
import strategies from "estol-auth";

import User from "your-user-model";

const { local, facebook, github, google, microsoft, twitter } = strategies;

// for local strategy auth with email & password
local(User, passport);

// for oauth strategies
facebook(User, passport, "your FB app id", "your FB app secret");
google(User, passport, "your GOOGLE app id", "your GOOGLE app secret");
// ... for any oauth strategy you need provide de same parameters (User model, passport, and oAuth service credentials)

// this functions will add the strategies to you local passport dependency and you need to export it after selecting your strategies
export default passport;
```

### Importing the module in your auth routes file

Import in your auth routes file the middlewares and controller functions this way

```javascript
import { controller, middlewares } from "estol-auth";

// here you can define constants for signIn & isAuthenticated middleware, and the user role's middlewares that you need
const { signIn, isAuthenticated, isModerator, isAdmin, isSuperAdmin } = middlewares;

// here you can define constants for controller functions
const { getOAuthUser, oauthService, signOut, signUp, updateUser } = controller;
```

#### Example

```javascript
import express from "express";
import { controller, middlewares } from "estol-auth";
import User from "your-user-model";
// import passport from your auth managment file with the strategies that you will be implementing in your app
import passport from "your-auth-management-file";

const { signInMiddleware, isAuthenticated, isModerator, isAdmin, isSuperAdmin } = middlewares;
const { getOAuthUser, oauthService, signOut, signUp, updateUser } = controller;

const router = express.Router();

// local routes
router.post("/local/sign-up", (req, res, next) => signUp(User, req, res, next));
router.post("/local/sign-in", signInMiddleware(passport), (req, res) => signIn(req, res));

// oauth routes
router.get("/oauth/success", isAuthenticated, getOauthUser);
router.get("/oauth/failure", (req, res) => res.redirect("your-failure-url"));
router.get("/oauth/get-user", isAuthenticated, getOAuthUser);
router.get("/oauth/:oauthService", (req, res, next) => passport.authenticate(req.params.oauthService)(req, res, next));
router.get("/oauth/:oauthService/callback", (req, res, next) => {
   passport.authenticate(req.params.oauthService, { failureRedirect: "/oauth/failure", successRedirect: "/oauth/success" });
});

// sign out function
router.get("/sign-out", isAuthenticated, signOut);
```

## IMPORTANT

You need to remmember to import and initialize passport from your auth management file in your index app file this way:

```javascript
import passport from "your-auth-management-file";

app.use(passport.initialize());
app.use(passport.session());
```

# Thanks for using this dependency

---
_Source: https://npm.io/package/estol-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
