Skip to content

Commit

Permalink
fix(auth): fix social authentication
Browse files Browse the repository at this point in the history
Fix callback handling after confirmation from third party application
  • Loading branch information
Frantz Kati committed Nov 21, 2020
1 parent 0e553e5 commit 4e6fc9b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
20 changes: 6 additions & 14 deletions examples/blog/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = tensei()
.dashboardPath('tensei')
.resources([Tag, Post, User, Comment])
.clientUrl('https://google.com')
.serverUrl('http://localhost:5000')
.defaultStorageDriver('local')
.routes([
route('Get products')
Expand Down Expand Up @@ -75,18 +76,9 @@ module.exports = tensei()
}),
])
.databaseConfig({
type: 'mysql',
dbName: 'mikrotensei',
// debug: true,
// user: 'mikrotensei',
// password: 'password',

// type: 'sqlite',
// dbName: 'mikrotensei',

// type: 'postgresql',
// // debug: true,
// dbName: 'bahdcoder',
// user: 'bahdcoder',
// password: 'bahdcoder'
type: process.env.DATABASE_TYPE || 'mysql',
dbName: process.env.DATABASE_NAME || 'mikrotensei',
debug: process.env.DEBUG || false,
user: process.env.DATABASE_USER || 'mikrotensei',
password: process.env.DATABASE_PASSWORD || '',
})
12 changes: 9 additions & 3 deletions packages/auth/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export type SupportedSocialProviders =
| 'twitter'
| 'linkedin'

export type AuthResources = {
user: ResourceContract
team: ResourceContract
role: ResourceContract
oauthIdentity: ResourceContract
permission: ResourceContract
teamInvite: ResourceContract
passwordReset: ResourceContract
}
export interface AuthPluginConfig {
fields: FieldContract[]
profilePictures: boolean
Expand Down Expand Up @@ -56,9 +65,6 @@ export interface AuthPluginConfig {
providers: {
[key: string]: GrantConfig
}
resources: {
[key: string]: ResourceContract
}
}

export interface UserEntity extends AnyEntity {
Expand Down
12 changes: 10 additions & 2 deletions packages/auth/src/controllers/SocialAuthCallbackController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import { RequestHandler } from 'express'
import purestConfig from '@purest/providers'
import { TensieContext, PluginSetupConfig } from '@tensei/common'
import AsyncHandler from 'express-async-handler'
import { AuthPluginConfig, SupportedSocialProviders } from 'config'
import {
AuthPluginConfig,
SupportedSocialProviders,
AuthResources
} from '../config'

const purest = Purest({ request: Request })

class SocialAuthCallbackController {
public connect = (authConfig: AuthPluginConfig): RequestHandler =>
public connect = (
authConfig: AuthPluginConfig & {
resources: AuthResources
}
): RequestHandler =>
AsyncHandler(async (request, response) => {
const { query, params, manager } = request

Expand Down
49 changes: 24 additions & 25 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import {
AuthData,
GrantConfig,
AuthResources,
AuthPluginConfig,
SupportedSocialProviders,
defaultProviderScopes
Expand Down Expand Up @@ -59,15 +60,6 @@ type JwtPayload = {
refresh?: boolean
}

type AuthResources = {
user: ResourceContract
team: ResourceContract
role: ResourceContract
oauthIdentity: ResourceContract
permission: ResourceContract
teamInvite: ResourceContract
passwordReset: ResourceContract
}
type AuthSetupFn = (resources: AuthResources) => any

class Auth {
Expand Down Expand Up @@ -95,8 +87,7 @@ class Auth {
verifyEmails: false,
skipWelcomeEmail: false,
rolesAndPermissions: false,
providers: {},
resources: {}
providers: {}
}

private resources: {
Expand Down Expand Up @@ -560,7 +551,10 @@ class Auth {

app.get(
`/${this.config.apiPath}/:provider/callback`,
SocialAuthCallbackController.connect(this.config)
SocialAuthCallbackController.connect({
...this.config,
resources: this.resources
})
)
}

Expand Down Expand Up @@ -809,9 +803,11 @@ class Auth {
.path(this.getApiPath('me'))
.get()
.handle(async ({ user }, { formatter: { ok, unauthorized } }) =>
user && ! user.public ? ok(user) : unauthorized({
message: 'Unauthorized.'
})
user && !user.public
? ok(user)
: unauthorized({
message: 'Unauthorized.'
})
),
route(`Resend Verification email`)
.path(this.getApiPath('verification/resend'))
Expand Down Expand Up @@ -840,17 +836,20 @@ class Auth {
route('Refresh Token')
.path(this.getApiPath('refresh-token'))
.post()
.handle(async (request, { formatter: { ok, unauthorized } }) => {
try {
return ok(
await this.handleRefreshTokens(request as any)
)
} catch (error) {
return unauthorized({
message: error.message || 'Invalid refresh token.'
})
.handle(
async (request, { formatter: { ok, unauthorized } }) => {
try {
return ok(
await this.handleRefreshTokens(request as any)
)
} catch (error) {
return unauthorized({
message:
error.message || 'Invalid refresh token.'
})
}
}
}),
),
route('Remove refresh Token')
.path(this.getApiPath('refresh-token'))
.delete()
Expand Down

0 comments on commit 4e6fc9b

Please sign in to comment.