@tsed/passport

Passport package for Ts.ED framework

Usage no npm install needed!

<script type="module">
  import tsedPassport from 'https://cdn.skypack.dev/@tsed/passport';
</script>

README

Ts.ED logo

Passport.js

Build & Release PR Welcome Coverage Status npm version semantic-release code style: prettier backers

Website   •   Getting started   •   Slack   •   Twitter

A package of Ts.ED framework. See website: https://tsed.io/#/tutorials/passport

Installation

Run npm command (or yarn):

npm install --save @tsed/passport passport
npm install --save-dev @types/passport

Configure your server

Add this configuration to your server:

import {Configuration} from "@tsed/common";

@Configuration({
  componentsScan: [
    `./protocols/*.ts` // scan protocols directory
  ],
  passport: {}
})
export class Server {}

Create a new Protocol

A Protocol is a special Ts.ED service which is used to declare a Passport Strategy and handle Passport lifecycle.

Here an example with the PassportLocal:

import {BodyParams, Req, Format, Required} from "@tsed/common";
import {Strategy} from "passport-local";
import {Unauthorized} from "@tsed/exceptions";
import {Protocol, OnInstall, OnVerify} from "@tsed/passport";
import {Inject} from "@tsed/di";
import {UserService} from "../services/UserService";

export class Credentials {
  @Required()
  @Format("email")
  email: string;

  @Required()
  password: string;
}

@Protocol({
  name: "login",
  useStrategy: Strategy,
  settings: {
    usernameField: "email",
    passwordField: "password"
  }
})
export class LocalProtocol implements OnVerify, OnInstall {
  @Inject(UserService)
  private userService: UserService;

  async $onVerify(@Req() request: Req, @BodyParams() credentials: Credentials) {
    const user = await this.userService.find(credentials);

    if (!user) {
      throw new Unauthorized("Unauthorized user");
    }

    if (!user.verifyPassword()) {
      throw new Unauthorized("Unauthorized user");
    }

    return user;
  }

  $onInstall(strategy: Strategy): void {
    // intercept the strategy instance to adding extra configuration
  }
}

Create the Passport controller

Create a new Passport controller as following:

import {BodyParams, Controller, Get, Post, ProviderScope, Req, Scope} from "@tsed/common";
import {Authenticate} from "@tsed/passport";

@Controller("/")
@Scope(ProviderScope.SINGLETON)
export class PassportCtrl {
  @Post("/login")
  @Authenticate("login")
  login(@Req() req: Req, @BodyParams("email") email: string, @BodyParams("password") password: string) {
    // FACADE
    return req.user;
  }
}

This controller will provide required all endpoints which will be used by the different protocols.

See our complete example on Ts.ED passport repository.

Contributors

Please read contributing guidelines here

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The MIT License (MIT)

Copyright (c) 2016 - 2018 Romain Lenzotti

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.