Add twitter strategy

This commit is contained in:
Onuwa Nnachi Isaac
2022-07-18 15:16:06 +01:00
parent 3d51b7d4d6
commit 7c19274fc5
6 changed files with 85 additions and 8 deletions

View File

@@ -1,17 +1,20 @@
MONGO_DATABASE=""
# DATABASE
MONGO_DATABASE="zk_groups_dev"
MONGO_USERNAME=""
MONGO_PASSWORD=""
MONGO_PORT=27017
# DOCKER
DOMAIN=semaphore_protocol
DEPLOY_COLOR
DEPLOY_COLOR=""
DOCKER_REMOTE_CONTEXT=""
BASE_URL="http://localhost:3333"
# Passport
# GITHUB Passport
GITHUB_CLIENT_ID=""
GITHUB_CLIENT_SECRET=""
# TWITTER Passport
TWITTER_CLIENT_ID=""
TWITTER_CLIENT_SECRET=""

View File

@@ -2,7 +2,7 @@ import { MiddlewareConsumer, Module, NestModule } from "@nestjs/common"
import { PassportModule } from "@nestjs/passport"
import { AuthController } from "./auth.controller"
import * as passport from "passport"
import { GithubStrategy } from "./strategy"
import { GithubStrategy, TwitterStrategy } from "./strategy"
import { AuthService } from "./auth.service"
import { AccountService } from "../account/account.service"
import { AccountModule } from "../account/account.module"
@@ -10,7 +10,13 @@ import { CookieSerializer } from "../common/cookie.serializer"
@Module({
imports: [AccountModule, PassportModule.register({ session: true })],
providers: [GithubStrategy, AuthService, AccountService, CookieSerializer],
providers: [
GithubStrategy,
TwitterStrategy,
AuthService,
AccountService,
CookieSerializer
],
controllers: [AuthController]
})
export class AuthModule implements NestModule {

View File

@@ -1 +1,2 @@
export * from "./github.strategy"
export * from "./twitter.strategy"

View File

@@ -0,0 +1,37 @@
import { Injectable, Logger } from "@nestjs/common"
import { PassportStrategy } from "@nestjs/passport"
import { Strategy } from "passport-twitter"
import { AccountService } from "../../account/account.service"
@Injectable()
export class TwitterStrategy extends PassportStrategy(Strategy) {
logger = new Logger(this.constructor.name)
constructor(private readonly accountService: AccountService) {
super({
clientID: process.env.TWITTER_CLIENT_ID,
clientSecret: process.env.TWITTER_CLIENT_SECRET,
callbackURL: `${process.env.BASE_URL}/api/auth/twitter`,
profileFields: ["id", "email", "read:user", "user:email"]
})
}
async validate(accessToken: string, refreshToken: string, profile) {
if (profile) {
const payload = {
service: "twitter",
tokens: {
accessToken,
userId: profile.id
},
username: profile.username,
fullName: profile.displayName,
avatarURL: profile.photos[0].value
}
const user = await this.accountService.create(payload)
return user.id
}
}
}

View File

@@ -28,6 +28,7 @@
"mongodb": "^4.8.0",
"passport": "^0.6.0",
"passport-github": "^1.1.0",
"passport-twitter": "^1.0.4",
"react": "18.1.0",
"react-dom": "18.1.0",
"react-router-dom": "6.3.0",

View File

@@ -8323,6 +8323,15 @@ passport-github@^1.1.0:
dependencies:
passport-oauth2 "1.x.x"
passport-oauth1@1.x.x:
version "1.2.0"
resolved "https://registry.yarnpkg.com/passport-oauth1/-/passport-oauth1-1.2.0.tgz#5229d431781bf5b265bec86ce9a9cce58a756cf9"
integrity sha512-Sv2YWodC6jN12M/OXwmR4BIXeeIHjjbwYTQw4kS6tHK4zYzSEpxBgSJJnknBjICA5cj0ju3FSnG1XmHgIhYnLg==
dependencies:
oauth "0.9.x"
passport-strategy "1.x.x"
utils-merge "1.x.x"
passport-oauth2@1.x.x:
version "1.6.1"
resolved "https://registry.yarnpkg.com/passport-oauth2/-/passport-oauth2-1.6.1.tgz#c5aee8f849ce8bd436c7f81d904a3cd1666f181b"
@@ -8339,6 +8348,14 @@ passport-strategy@1.x.x:
resolved "https://registry.yarnpkg.com/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4"
integrity sha512-CB97UUvDKJde2V0KDWWB3lyf6PC3FaZP7YxZ2G8OAtn9p4HI9j9JLP9qjOGZFvyl8uwNT8qM+hGnz/n16NI7oA==
passport-twitter@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/passport-twitter/-/passport-twitter-1.0.4.tgz#01a799e1f760bf2de49f2ba5fba32282f18932d7"
integrity sha512-qvdauqCqCJJci82mJ9hZZQ6nAv7aSHV31svL8+9H7mRlDdXCdfU6AARQrmmJu3DRmv9fvIebM7zzxR7mVufN3A==
dependencies:
passport-oauth1 "1.x.x"
xtraverse "0.1.x"
passport@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/passport/-/passport-0.6.0.tgz#e869579fab465b5c0b291e841e6cc95c005fac9d"
@@ -10996,11 +11013,23 @@ xmlchars@^2.2.0:
resolved "https://registry.yarnpkg.com/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb"
integrity sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==
xmldom@0.1.x:
version "0.1.31"
resolved "https://registry.yarnpkg.com/xmldom/-/xmldom-0.1.31.tgz#b76c9a1bd9f0a9737e5a72dc37231cf38375e2ff"
integrity sha512-yS2uJflVQs6n+CyjHoaBmVSqIDevTAWrzMmjG1Gc7h1qQ7uVozNhEPJAwZXWyGQ/Gafo3fCwrcaokezLPupVyQ==
xtend@^4.0.0:
version "4.0.2"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==
xtraverse@0.1.x:
version "0.1.0"
resolved "https://registry.yarnpkg.com/xtraverse/-/xtraverse-0.1.0.tgz#b741bad018ef78d8a9d2e83ade007b3f7959c732"
integrity sha512-MANQdlG2hl1nQobxz1Rv8hsS1RuBS0C1N6qTOupv+9vmfrReePdxhmB2ecYjvsp4stJ80HD7erjkoF1Hd/FK9A==
dependencies:
xmldom "0.1.x"
y18n@^5.0.5:
version "5.0.8"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55"