3
3
import * as y from "yoctocolors"
4
4
import open from "open"
5
5
import clipboard from "clipboardy"
6
- import { select , input , password } from "@inquirer/prompts"
6
+ import { select , input , password , number } from "@inquirer/prompts"
7
7
import { requireFramework } from "../lib/detect.js"
8
8
import { updateEnvFile } from "../lib/write-env.js"
9
9
import { providers , frameworks } from "../lib/meta.js"
10
- import { secret } from "./index.js"
11
10
import { link , markdownToAnsi } from "../lib/markdown.js"
11
+ import { appleGenSecret } from "../lib/apple-gen-secret.js"
12
+
13
+ /**
14
+ * @param {string } label
15
+ * @param {string } [defaultValue]
16
+ */
17
+ async function promptInput ( label , defaultValue ) {
18
+ return input ( {
19
+ message : `Paste ${ y . magenta ( label ) } :` ,
20
+ validate : ( value ) => ! ! value ,
21
+ default : defaultValue ,
22
+ } )
23
+ }
24
+
25
+ /** @param {string } label */
26
+ async function promptPassword ( label ) {
27
+ return password ( {
28
+ message : `Paste ${ y . magenta ( label ) } :` ,
29
+ mask : true ,
30
+ validate : ( value ) => ! ! value ,
31
+ } )
32
+ }
12
33
13
34
const choices = Object . entries ( providers )
14
35
. filter ( ( [ , { setupUrl } ] ) => ! ! setupUrl )
@@ -17,19 +38,19 @@ const choices = Object.entries(providers)
17
38
/** @param {string | undefined } providerId */
18
39
export async function action ( providerId ) {
19
40
try {
20
- if ( ! providerId ) {
21
- providerId = await select ( {
41
+ const pid =
42
+ providerId ??
43
+ ( await select ( {
22
44
message : "What provider do you want to set up?" ,
23
45
choices : choices ,
24
- } )
25
- }
46
+ } ) )
26
47
27
- const provider = providers [ providerId ]
48
+ const provider = providers [ pid ]
28
49
if ( ! provider ?. setupUrl ) {
29
50
console . error (
30
51
y . red (
31
52
`Missing instructions for ${
32
- provider ?. name ?? providerId
53
+ provider ?. name ?? pid
33
54
} .\nInstructions are available for: ${ y . bold (
34
55
choices . map ( ( choice ) => choice . name ) . join ( ", " )
35
56
) } `
@@ -78,35 +99,48 @@ ${y.bold("Callback URL (copied to clipboard)")}: ${url}`
78
99
79
100
await open ( provider . setupUrl )
80
101
81
- const clientId = await input ( {
82
- message : `Paste ${ y . magenta ( "Client ID" ) } :` ,
83
- validate : ( value ) => ! ! value ,
84
- } )
85
- const clientSecret = await password ( {
86
- message : `Paste ${ y . magenta ( "Client secret" ) } :` ,
87
- mask : true ,
88
- validate : ( value ) => ! ! value ,
89
- } )
102
+ if ( providerId === "apple" ) {
103
+ const clientId = await promptInput ( "Client ID" )
104
+ const keyId = await promptInput ( "Key ID" )
105
+ const teamId = await promptInput ( "Team ID" )
106
+ const privateKey = await input ( {
107
+ message : "Path to Private Key" ,
108
+ validate : ( value ) => ! ! value ,
109
+ default : "./private-key.p8" ,
110
+ } )
90
111
91
- console . log ( y . dim ( `Updating environment variable file...` ) )
112
+ const expiresInDays =
113
+ ( await number ( {
114
+ message : "Expires in days (default: 180)" ,
115
+ required : false ,
116
+ default : 180 ,
117
+ } ) ) ?? 180
92
118
93
- const varPrefix = `AUTH_ ${ providerId . toUpperCase ( ) } `
119
+ console . log ( y . dim ( "Updating environment variable file..." ) )
94
120
95
- await updateEnvFile ( {
96
- [ `${ varPrefix } _ID` ] : clientId ,
97
- [ `${ varPrefix } _SECRET` ] : clientSecret ,
98
- } )
121
+ await updateEnvFile ( { AUTH_APPLE_ID : clientId } )
99
122
100
- console . log (
101
- y . dim (
102
- `\nEnsuring that ${ link (
103
- "AUTH_SECRET" ,
104
- "https://authjs.dev/getting-started/installation#setup-environment"
105
- ) } is set...`
106
- )
107
- )
123
+ const secret = await appleGenSecret ( {
124
+ teamId,
125
+ clientId,
126
+ keyId,
127
+ privateKey,
128
+ expiresInDays,
129
+ } )
130
+
131
+ await updateEnvFile ( { AUTH_APPLE_SECRET : secret } )
132
+ } else {
133
+ const clientId = await promptInput ( "Client ID" )
134
+ const clientSecret = await promptPassword ( "Client Secret" )
108
135
109
- await secret . action ( { } )
136
+ console . log ( y . dim ( "Updating environment variable file..." ) )
137
+
138
+ const varPrefix = `AUTH_${ pid . toUpperCase ( ) } `
139
+ await updateEnvFile ( {
140
+ [ `${ varPrefix } _ID` ] : clientId ,
141
+ [ `${ varPrefix } _SECRET` ] : clientSecret ,
142
+ } )
143
+ }
110
144
111
145
console . log ( "\n🎉 Done! You can now use this provider in your app." )
112
146
} catch ( error ) {
0 commit comments