-
-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathindex.ts
More file actions
126 lines (116 loc) · 3.3 KB
/
index.ts
File metadata and controls
126 lines (116 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import chalk from 'chalk'
import { generator, runGenerators, prompt, install } from '@feathershq/pinion'
import {
addVersions,
checkPreconditions,
FeathersBaseContext,
getDatabaseAdapter,
initializeBaseContext
} from '../commons'
import { generate as serviceGenerator, ServiceGeneratorContext } from '../service/index'
export interface AuthenticationGeneratorContext extends ServiceGeneratorContext {
service: string
entity: string
authStrategies: string[]
dependencies: string[]
}
export type AuthenticationGeneratorArguments = FeathersBaseContext &
Partial<Pick<AuthenticationGeneratorContext, 'service' | 'authStrategies' | 'entity' | 'path'>>
export const localTemplate = (authStrategies: string[], content: string) =>
authStrategies.includes('local') ? content : ''
export const oauthTemplate = (authStrategies: string[], content: string) =>
authStrategies.filter((s) => s !== 'local').length > 0 ? content : ''
export const prompts = (ctx: AuthenticationGeneratorArguments) => [
{
type: 'checkbox',
name: 'authStrategies',
when: !ctx.authStrategies,
message: 'Which authentication methods do you want to use?',
suffix: chalk.grey(' Other methods and providers can be added at any time.'),
choices: [
{
name: 'Email + Password',
value: 'local',
checked: true
},
{
name: 'Google',
value: 'google'
},
{
name: 'Facebook',
value: 'facebook'
},
{
name: 'Twitter',
value: 'twitter'
},
{
name: 'GitHub',
value: 'github'
},
{
name: 'Auth0',
value: 'auth0'
}
]
},
{
name: 'service',
type: 'input',
when: !ctx.service,
message: 'What is your authentication service name?',
default: 'user'
},
{
name: 'path',
type: 'input',
when: !ctx.path,
message: 'What path should the service be registered on?',
default: 'users'
},
{
name: 'entity',
type: 'input',
when: !ctx.entity,
message: 'What is your authenticated entity name?',
suffix: chalk.grey(' Will be available in params (e.g. params.user)'),
default: 'user'
}
]
export const generate = (ctx: AuthenticationGeneratorArguments) =>
generator(ctx)
.then(initializeBaseContext())
.then(checkPreconditions())
.then(prompt<AuthenticationGeneratorArguments, AuthenticationGeneratorContext>(prompts))
.then(async (ctx) => {
const serviceContext = await serviceGenerator({
...ctx,
name: ctx.service,
isEntityService: true,
type: getDatabaseAdapter(ctx.feathers?.database)
})
return {
...ctx,
...serviceContext
}
})
.then(runGenerators(__dirname, 'templates'))
.then((ctx) => {
const dependencies: string[] = []
dependencies.push('@feathersjs/authentication-oauth')
if (ctx.authStrategies.includes('local')) {
dependencies.push('@feathersjs/authentication-local')
}
if (ctx.dependencies) {
return {
...ctx,
dependencies: [...ctx.dependencies, ...dependencies]
}
}
return install<AuthenticationGeneratorContext>(
addVersions(dependencies, ctx.dependencyVersions),
false,
ctx.feathers.packager
)(ctx)
})