Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
feat (import): added readme and license, and added np
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgalbu committed Oct 12, 2021
1 parent 741a3b5 commit 58f8162
Show file tree
Hide file tree
Showing 6 changed files with 6,372 additions and 2,184 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
build
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
message="chore(release): %s"
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The MIT License

Copyright 2021 Massimo Galbusera, contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# adonis5-jwt

[![npm-image]][npm-url] [![license-image]][license-url] [![typescript-image]][typescript-url]

Add JWT authentication to Adonisjs v5.

## Installation

Install via `npm` or `yarn`:

```js
npm install adonis5-jwt
//Or if you use yarn
yarn add adonis5-jwt
```

Edit `contracts/auth.ts` like this:

```ts
//Add the following line
import { JWTGuardConfig, JWTGuardContract } from "@ioc:Adonisjs/Addons/Jwt";

declare module '@ioc:Adonis/Addons/Auth' {
...

interface GuardsList {
...other guards...

//Add the following lines
jwt: {
implementation: JWTGuardContract<'user', 'jwt'>,
config: JWTGuardConfig<'user'>,
}
}
}
```

## Usage

JWT authentication implements the same methods that other guards in `@adonisjs/auth` implements, so you can call `.authenticate()`, `.generate()` etc.

Just make sure to prepend `.use("jwt")`:

```ts
//authenticate() example
Route.get('/dashboard', async ({ auth }) => {
await auth.use("jwt").authenticate();
const userPayload = auth.use("jwt").user!;
});

//generate() example:
Route.get('/login', async ({ auth }) => {
const user = await User.find(1);
const jwt = await auth.use("jwt").generate(user);
});

Route.post('/logout', async ({ auth, response }) => {
await auth.use('jwt').revoke()
return {
revoked: true
}
})
```

By default, `.generate()` uses a payload like the following:

```ts
//user is a Lucid model
{
userId: user.id,
user: {
name: user.name,
email: user.email,
},
}
```

If you want to generate a JWT with a different payload, simply specify `payload` when calling `.generate()`:

```ts
await auth.use("jwt").generate(user, {
payload: {
email: user.email,
},
});
```

[npm-image]: https://img.shields.io/npm/v/adonis-jwt.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/adonis-jwt "npm"

[license-image]: https://img.shields.io/npm/l/adonis-jwt?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"

[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
Loading

0 comments on commit 58f8162

Please sign in to comment.