Skip to content

Commit 4e6fc9b

Browse files
author
Frantz Kati
committed
fix(auth): fix social authentication
Fix callback handling after confirmation from third party application
1 parent 0e553e5 commit 4e6fc9b

File tree

4 files changed

+49
-44
lines changed

4 files changed

+49
-44
lines changed

examples/blog/app.js

+6-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = tensei()
1313
.dashboardPath('tensei')
1414
.resources([Tag, Post, User, Comment])
1515
.clientUrl('https://google.com')
16+
.serverUrl('http://localhost:5000')
1617
.defaultStorageDriver('local')
1718
.routes([
1819
route('Get products')
@@ -75,18 +76,9 @@ module.exports = tensei()
7576
}),
7677
])
7778
.databaseConfig({
78-
type: 'mysql',
79-
dbName: 'mikrotensei',
80-
// debug: true,
81-
// user: 'mikrotensei',
82-
// password: 'password',
83-
84-
// type: 'sqlite',
85-
// dbName: 'mikrotensei',
86-
87-
// type: 'postgresql',
88-
// // debug: true,
89-
// dbName: 'bahdcoder',
90-
// user: 'bahdcoder',
91-
// password: 'bahdcoder'
79+
type: process.env.DATABASE_TYPE || 'mysql',
80+
dbName: process.env.DATABASE_NAME || 'mikrotensei',
81+
debug: process.env.DEBUG || false,
82+
user: process.env.DATABASE_USER || 'mikrotensei',
83+
password: process.env.DATABASE_PASSWORD || '',
9284
})

packages/auth/src/config.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ export type SupportedSocialProviders =
2525
| 'twitter'
2626
| 'linkedin'
2727

28+
export type AuthResources = {
29+
user: ResourceContract
30+
team: ResourceContract
31+
role: ResourceContract
32+
oauthIdentity: ResourceContract
33+
permission: ResourceContract
34+
teamInvite: ResourceContract
35+
passwordReset: ResourceContract
36+
}
2837
export interface AuthPluginConfig {
2938
fields: FieldContract[]
3039
profilePictures: boolean
@@ -56,9 +65,6 @@ export interface AuthPluginConfig {
5665
providers: {
5766
[key: string]: GrantConfig
5867
}
59-
resources: {
60-
[key: string]: ResourceContract
61-
}
6268
}
6369

6470
export interface UserEntity extends AnyEntity {

packages/auth/src/controllers/SocialAuthCallbackController.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ import { RequestHandler } from 'express'
77
import purestConfig from '@purest/providers'
88
import { TensieContext, PluginSetupConfig } from '@tensei/common'
99
import AsyncHandler from 'express-async-handler'
10-
import { AuthPluginConfig, SupportedSocialProviders } from 'config'
10+
import {
11+
AuthPluginConfig,
12+
SupportedSocialProviders,
13+
AuthResources
14+
} from '../config'
1115

1216
const purest = Purest({ request: Request })
1317

1418
class SocialAuthCallbackController {
15-
public connect = (authConfig: AuthPluginConfig): RequestHandler =>
19+
public connect = (
20+
authConfig: AuthPluginConfig & {
21+
resources: AuthResources
22+
}
23+
): RequestHandler =>
1624
AsyncHandler(async (request, response) => {
1725
const { query, params, manager } = request
1826

packages/auth/src/index.ts

+24-25
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import {
3232
AuthData,
3333
GrantConfig,
34+
AuthResources,
3435
AuthPluginConfig,
3536
SupportedSocialProviders,
3637
defaultProviderScopes
@@ -59,15 +60,6 @@ type JwtPayload = {
5960
refresh?: boolean
6061
}
6162

62-
type AuthResources = {
63-
user: ResourceContract
64-
team: ResourceContract
65-
role: ResourceContract
66-
oauthIdentity: ResourceContract
67-
permission: ResourceContract
68-
teamInvite: ResourceContract
69-
passwordReset: ResourceContract
70-
}
7163
type AuthSetupFn = (resources: AuthResources) => any
7264

7365
class Auth {
@@ -95,8 +87,7 @@ class Auth {
9587
verifyEmails: false,
9688
skipWelcomeEmail: false,
9789
rolesAndPermissions: false,
98-
providers: {},
99-
resources: {}
90+
providers: {}
10091
}
10192

10293
private resources: {
@@ -560,7 +551,10 @@ class Auth {
560551

561552
app.get(
562553
`/${this.config.apiPath}/:provider/callback`,
563-
SocialAuthCallbackController.connect(this.config)
554+
SocialAuthCallbackController.connect({
555+
...this.config,
556+
resources: this.resources
557+
})
564558
)
565559
}
566560

@@ -809,9 +803,11 @@ class Auth {
809803
.path(this.getApiPath('me'))
810804
.get()
811805
.handle(async ({ user }, { formatter: { ok, unauthorized } }) =>
812-
user && ! user.public ? ok(user) : unauthorized({
813-
message: 'Unauthorized.'
814-
})
806+
user && !user.public
807+
? ok(user)
808+
: unauthorized({
809+
message: 'Unauthorized.'
810+
})
815811
),
816812
route(`Resend Verification email`)
817813
.path(this.getApiPath('verification/resend'))
@@ -840,17 +836,20 @@ class Auth {
840836
route('Refresh Token')
841837
.path(this.getApiPath('refresh-token'))
842838
.post()
843-
.handle(async (request, { formatter: { ok, unauthorized } }) => {
844-
try {
845-
return ok(
846-
await this.handleRefreshTokens(request as any)
847-
)
848-
} catch (error) {
849-
return unauthorized({
850-
message: error.message || 'Invalid refresh token.'
851-
})
839+
.handle(
840+
async (request, { formatter: { ok, unauthorized } }) => {
841+
try {
842+
return ok(
843+
await this.handleRefreshTokens(request as any)
844+
)
845+
} catch (error) {
846+
return unauthorized({
847+
message:
848+
error.message || 'Invalid refresh token.'
849+
})
850+
}
852851
}
853-
}),
852+
),
854853
route('Remove refresh Token')
855854
.path(this.getApiPath('refresh-token'))
856855
.delete()

0 commit comments

Comments
 (0)