# @open-stock/stock-auth-server

> > TODO: description

Latest version **4.2.310** (published 2024-11-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install @open-stock/stock-auth-server
pnpm add @open-stock/stock-auth-server
yarn add @open-stock/stock-auth-server
bun add @open-stock/stock-auth-server
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.2.310 |
| Published | 2024-11-19 |
| First published | 2023-05-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 358.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | BrianPollar |
| Maintainers | pollarbrian |

## Links

- npm: https://www.npmjs.com/package/@open-stock/stock-auth-server
- npm.io page: https://npm.io/package/@open-stock/stock-auth-server

## Dependencies (8)

- [authy](https://npm.io/package/authy.md) ^1.4.0
- [bcrypt](https://npm.io/package/bcrypt.md) ^5.1.1
- [log4js](https://npm.io/package/log4js.md) ^6.9.1
- [passport](https://npm.io/package/passport.md) ^0.6.0
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.0
- [passport-jwt](https://npm.io/package/passport-jwt.md) ^4.0.1
- [passport-google-oauth](https://npm.io/package/passport-google-oauth.md) ^2.0.0
- [mongoose-unique-validator](https://npm.io/package/mongoose-unique-validator.md) ^4.0.0

## Recent versions

- 4.2.310 (latest) — 2024-11-19
- 4.2.309 — 2024-11-19
- 4.2.308 — 2024-11-19
- 4.2.307 — 2024-11-19
- 4.2.306 — 2024-11-19
- 4.2.305 — 2024-11-19
- 4.2.304 — 2024-11-19
- 4.2.303 — 2024-11-19
- 4.2.302 — 2024-11-19
- 4.2.301 — 2024-11-19
- 4.2.300 — 2024-11-19
- 4.2.299 — 2024-11-19
- 4.2.298 — 2024-11-19
- 4.2.297 — 2024-11-19
- 4.2.296 — 2024-11-19
- … 497 more at https://npm.io/package/@open-stock/stock-auth-server/versions

## README

# @open-stock/stock-auth-server

A simple and powerfull authentication library for Nodejs with user state management.
This only works with (https://github.com/BrianPollar/@open-stock/stock-auth-client) the server library.
This requires @open-stock/stock-notif-server and
@open-stock/stock-universal-server to be installed and initialised.

## Features

stock-auth-server implements a full authentication system. It consists of:

- a Node Js Server library (this repository)
- a [JavaScript library](https://github.com/BrianPollar/@open-stock/stock-auth-client) for Node.js.

Its main features are:

#### Reliability

The libarary uses standard encryption for data protection:

#### Simple and convenient API

Sample code:

```ts
import expresss from 'expresss';
import { runStockAuthServer, IStockAuthServerConfig } from "@open-stock/stock-auth-server";

const app = expresss;

const config : IStockAuthServerConfig {
  // ...config
}
const { stockAuthRouter } = await runStockAuthServer(config);

app.use(stockAuthRouter);
```

## Installation

```bash
// with npm
npm install @open-stock/stock-auth-server

// with yarn
yarn add @open-stock/stock-auth-server
```

## How to use

The following example initialises stock-auth-client and gets the chat client and chat controller instances.

```js
import expresss from 'expresss';
import { runStockAuthServer, IStockAuthServerConfig } from "@open-stock/stock-auth-server";

const app = expresss;

const config : IStockAuthServerConfig {
  // ...config
}
const { stockAuthRouter } = await runStockAuthServer(config);

app.use(stockAuthRouter);
```

## Extra Features

Here are the features of the stock-auth-server library

## NOTE

Most of the features below are automatically implememnted by the library. But If you want to use the classes and functions provided by the library, you can do so by importing them from the library.
Some of them are elaborated below.

## defineAdmin

defineAdmin is a function that is used to define an admin.

# usage

```ts
import { defineAdmin } from "@open-stock/stock-auth-server";
const adminProps = defineAdmin();
```

## login

login is a function that is used to login a admin.

# usage

```ts
import { login } from "@open-stock/stock-auth-server";

const res = await login(password, serverKey);
```

## checkIfAdmin

checkIfAdmin is a function that is used to check if a user is an admin.

# usage

```ts
import { checkIfAdmin } from "@open-stock/stock-auth-server";
const emailPhone = "email@mail.com" || "1234567890";
const password = "password";
const recorgnisewdServerId = "serverId";
const serverKey = "serverKey";

const res = await checkIfAdmin(
  emailPhone,
  password,
  recorgnisewdServerId,
  serverKey
);
```

## checkIpAndAttempt

checkIpAndAttempt is a middleware function that is used to check the ip and attempt.

# usage

```ts
import { checkIpAndAttempt } from "@open-stock/stock-auth-server";
import { apiRouter } from "@open-stock/stock-universal-server";

apiRouter.post('/login', checkIpAndAttempt(), (req, res) => {}));
```

## isTooCommonPhrase

isTooCommonPhrase is a middleware function that is used to check if a phrase is too common.

# usage

```ts
import { isTooCommonPhrase } from "@open-stock/stock-auth-server";
import { apiRouter } from "@open-stock/stock-universal-server";

apiRouter.post('/signup', isTooCommonPhrase(), (req, res) => {}));
```

## isInAdictionaryOnline

isInAdictionaryOnline is a middleware function that is used to check if a phrase is in a dictionary online.

# usage

```ts
import { isInAdictionaryOnline } from "@open-stock/stock-auth-server";
import { apiRouter } from "@open-stock/stock-universal-server";

apiRouter.post('/signup', isInAdictionaryOnline(), (req, res) => {}));
```

## generateToken

generateToken is a function that is used to generate a jwt token.

# usage

```ts
import { generateToken } from "@open-stock/stock-auth-server";
import { Iauthtoken } from "@open-stock/stock-universal";

const authConfig: Iauthtoken = {
  // ...
};
const expiryDate = new Date();
const jwtSecret = "string";

const token = generateToken(authConfig, expiryDate, jwtSecret);
```

## setUserInfo

setUserInfo is a function that is used to set the user info.

# usage

```ts
import { setUserInfo } from "@open-stock/stock-auth-server";

const userId: "string";
const permissions: Iuserperm = {
  // ...
};
const companyId: "string";
const companyPermissions: IcompanyPerm = {
  // ...
};

const details = setUserInfo(userId, permissions, companyId, companyPermissions);
```

## companyAuthRoutes

companyAuthRoutes is a function that is used to get the company auth routes.

# usage

```ts
import { companyAuthRoutes } from "@open-stock/stock-auth-server";
import { express } from "express";
const app = express();
app.use("/company", companyAuthRoutes());

// see all routes in the file the router is defined in
```

## authRoutes

authRoutes is a function that is used to get the auth routes.

# usage

```ts
import { authRoutes } from "@open-stock/stock-auth-server";
import { express } from "express";
const app = express();
app.use("/userauth", companyAuthRoutes());
```

## Documentation

The source code of the website can be found [here](https://github.com/BrianPollar/@open-stock/stock-auth-server). Contributions are welcome!

## Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site.

## License

[MIT](LICENSE)

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