@@ -4,55 +4,36 @@ import {
4
4
getSessionExpirationDate ,
5
5
getUserId ,
6
6
} from '../../utils/auth.server.ts'
7
+ import { handleMockCallback } from '../../utils/connections.server.ts'
8
+ import { ProviderNameSchema , providerLabels } from '../../utils/connections.tsx'
7
9
import { prisma } from '../../utils/db.server.ts'
8
- import { GITHUB_PROVIDER_NAME } from '../../utils/github-auth.server.ts'
9
10
import { combineHeaders } from '../../utils/misc.tsx'
10
11
import {
11
12
destroyRedirectToHeader ,
12
13
getRedirectCookieValue ,
13
14
} from '../../utils/redirect-cookie.server.ts'
14
- import { sessionStorage } from '../../utils/session.server.ts'
15
15
import {
16
16
createToastHeaders ,
17
17
redirectWithToast ,
18
18
} from '../../utils/toast.server.ts'
19
19
import { verifySessionStorage } from '../../utils/verification.server.ts'
20
20
import { handleNewSession } from './login.tsx'
21
21
import {
22
- githubIdKey ,
23
22
onboardingEmailSessionKey ,
24
23
prefilledProfileKey ,
25
- } from './onboarding_.github.tsx'
26
-
27
- export const ROUTE_PATH = '/auth/github/callback'
24
+ providerIdKey ,
25
+ } from './onboarding_.$provider.tsx'
28
26
29
27
const destroyRedirectTo = { 'set-cookie' : destroyRedirectToHeader }
30
28
31
- export async function loader ( { request } : DataFunctionArgs ) {
32
- const reqUrl = new URL ( request . url )
29
+ export async function loader ( { request, params } : DataFunctionArgs ) {
30
+ const providerName = ProviderNameSchema . parse ( params . provider )
31
+ request = await handleMockCallback ( providerName , request )
33
32
const redirectTo = getRedirectCookieValue ( request )
34
- debugger
35
-
36
- // normally you *really* want to avoid including test/dev code in your source
37
- // but this is one of those cases where it's worth it to make the dev
38
- // experience better. The fact is it's basically impossible to test these
39
- // kinds of integrations.
40
- if ( process . env . GITHUB_CLIENT_ID ?. startsWith ( 'MOCK_' ) ) {
41
- const cookieSession = await sessionStorage . getSession (
42
- request . headers . get ( 'cookie' ) ,
43
- )
44
- const state = cookieSession . get ( 'oauth2:state' ) ?? 'MOCK_STATE'
45
- cookieSession . set ( 'oauth2:state' , state )
46
- reqUrl . searchParams . set ( 'state' , state )
47
- request . headers . set (
48
- 'cookie' ,
49
- await sessionStorage . commitSession ( cookieSession ) ,
50
- )
51
- request = new Request ( reqUrl . toString ( ) , request )
52
- }
33
+ const label = providerLabels [ providerName ]
53
34
54
35
const authResult = await authenticator
55
- . authenticate ( GITHUB_PROVIDER_NAME , request , { throwOnError : true } )
36
+ . authenticate ( providerName , request , { throwOnError : true } )
56
37
. then (
57
38
data => ( { success : true , data } ) as const ,
58
39
error => ( { success : false , error } ) as const ,
@@ -64,7 +45,7 @@ export async function loader({ request }: DataFunctionArgs) {
64
45
'/login' ,
65
46
{
66
47
title : 'Auth Failed' ,
67
- description : ' There was an error authenticating with GitHub.' ,
48
+ description : ` There was an error authenticating with ${ label } .` ,
68
49
type : 'error' ,
69
50
} ,
70
51
{ headers : destroyRedirectTo } ,
@@ -87,7 +68,7 @@ export async function loader({ request }: DataFunctionArgs) {
87
68
'/settings/profile/connections' ,
88
69
{
89
70
title : 'Already Connected' ,
90
- description : `Your "${ profile . username } " GitHub account is already connected.` ,
71
+ description : `Your "${ profile . username } " ${ label } account is already connected.` ,
91
72
} ,
92
73
{ headers : destroyRedirectTo } ,
93
74
)
@@ -96,18 +77,18 @@ export async function loader({ request }: DataFunctionArgs) {
96
77
'/settings/profile/connections' ,
97
78
{
98
79
title : 'Already Connected' ,
99
- description : `The "${ profile . username } " GitHub account is already connected to another account.` ,
80
+ description : `The "${ profile . username } " ${ label } account is already connected to another account.` ,
100
81
} ,
101
82
{ headers : destroyRedirectTo } ,
102
83
)
103
84
}
104
85
}
105
86
106
- // If we're already logged in, then link the GitHub account
87
+ // If we're already logged in, then link the account
107
88
if ( userId ) {
108
89
await prisma . connection . create ( {
109
90
data : {
110
- providerName : GITHUB_PROVIDER_NAME ,
91
+ providerName,
111
92
providerId : profile . id ,
112
93
userId,
113
94
} ,
@@ -117,7 +98,7 @@ export async function loader({ request }: DataFunctionArgs) {
117
98
{
118
99
title : 'Connected' ,
119
100
type : 'success' ,
120
- description : `Your "${ profile . username } " GitHub account has been connected.` ,
101
+ description : `Your "${ profile . username } " ${ label } account has been connected.` ,
121
102
} ,
122
103
{ headers : destroyRedirectTo } ,
123
104
)
@@ -128,7 +109,7 @@ export async function loader({ request }: DataFunctionArgs) {
128
109
return makeSession ( { request, userId : existingConnection . userId } )
129
110
}
130
111
131
- // if the github email matches a user in the db, then link the account and
112
+ // if the email matches a user in the db, then link the account and
132
113
// make a new session
133
114
const user = await prisma . user . findUnique ( {
134
115
select : { id : true } ,
@@ -137,7 +118,7 @@ export async function loader({ request }: DataFunctionArgs) {
137
118
if ( user ) {
138
119
await prisma . connection . create ( {
139
120
data : {
140
- providerName : GITHUB_PROVIDER_NAME ,
121
+ providerName,
141
122
providerId : profile . id ,
142
123
userId : user . id ,
143
124
} ,
@@ -147,7 +128,7 @@ export async function loader({ request }: DataFunctionArgs) {
147
128
{
148
129
headers : await createToastHeaders ( {
149
130
title : 'Connected' ,
150
- description : `Your "${ profile . username } " GitHub account has been connected.` ,
131
+ description : `Your "${ profile . username } " ${ label } account has been connected.` ,
151
132
} ) ,
152
133
} ,
153
134
)
@@ -163,9 +144,9 @@ export async function loader({ request }: DataFunctionArgs) {
163
144
email : profile . email . toLowerCase ( ) ,
164
145
username : profile . username ?. replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_' ) . toLowerCase ( ) ,
165
146
} )
166
- verifySession . set ( githubIdKey , profile . id )
147
+ verifySession . set ( providerIdKey , profile . id )
167
148
const onboardingRedirect = [
168
- ' /onboarding/github' ,
149
+ ` /onboarding/${ providerName } ` ,
169
150
redirectTo ? new URLSearchParams ( { redirectTo } ) : null ,
170
151
]
171
152
. filter ( Boolean )
0 commit comments