Skip to content

Commit 95b8a0d

Browse files
committed
chore: upgrade remix-auth and remix-auth-github dependencies
Update authentication libraries to latest versions: - remix-auth from 3.7.0 to 4.1.0 - remix-auth-github from 1.7.0 to 3.0.2 Refactor authentication implementation to match new library requirements, including: - Removing connection session storage - Updating GitHub strategy configuration - Adjusting authentication callback handling
1 parent 09e2f33 commit 95b8a0d

File tree

7 files changed

+94
-81
lines changed

7 files changed

+94
-81
lines changed

app/routes/_auth+/auth.$provider.callback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function loader({ request, params }: Route.LoaderArgs) {
3838
const label = providerLabels[providerName]
3939

4040
const authResult = await authenticator
41-
.authenticate(providerName, request, { throwOnError: true })
41+
.authenticate(providerName, request)
4242
.then(
4343
(data) =>
4444
({

app/utils/auth.server.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import bcrypt from 'bcryptjs'
33
import { redirect } from 'react-router'
44
import { Authenticator } from 'remix-auth'
55
import { safeRedirect } from 'remix-utils/safe-redirect'
6-
import { connectionSessionStorage, providers } from './connections.server.ts'
6+
import { providers } from './connections.server.ts'
77
import { prisma } from './db.server.ts'
88
import { combineHeaders, downloadFile } from './misc.tsx'
99
import { type ProviderUser } from './providers/provider.ts'
@@ -15,9 +15,7 @@ export const getSessionExpirationDate = () =>
1515

1616
export const sessionKey = 'sessionId'
1717

18-
export const authenticator = new Authenticator<ProviderUser>(
19-
connectionSessionStorage,
20-
)
18+
export const authenticator = new Authenticator<ProviderUser>()
2119

2220
for (const [providerName, provider] of Object.entries(providers)) {
2321
authenticator.use(provider.getAuthStrategy(), providerName)

app/utils/providers/github.server.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SetCookie } from '@mjackson/headers'
12
import { createId as cuid } from '@paralleldrive/cuid2'
23
import { redirect } from 'react-router'
34
import { GitHubStrategy } from 'remix-auth-github'
@@ -28,24 +29,33 @@ export class GitHubProvider implements AuthProvider {
2829
getAuthStrategy() {
2930
return new GitHubStrategy(
3031
{
31-
clientID: process.env.GITHUB_CLIENT_ID,
32+
clientId: process.env.GITHUB_CLIENT_ID,
3233
clientSecret: process.env.GITHUB_CLIENT_SECRET,
33-
callbackURL: '/auth/github/callback',
34+
redirectURI: '/auth/github/callback',
3435
},
35-
async ({ profile }) => {
36-
const email = profile.emails[0]?.value.trim().toLowerCase()
36+
async ({ tokens }) => {
37+
const response = await fetch('https://api.github.com/user', {
38+
headers: {
39+
Accept: 'application/vnd.github+json',
40+
Authorization: `Bearer ${tokens.accessToken()}`,
41+
'X-GitHub-Api-Version': '2022-11-28',
42+
},
43+
})
44+
const profile = (await response.json()) as any
45+
const email = profile.emails[0]?.trim().toLowerCase()
3746
if (!email) {
3847
throw new Error('Email not found')
3948
}
40-
const username = profile.displayName
41-
const imageUrl = profile.photos[0]?.value
42-
return {
43-
email,
49+
// const username = profile.displayName
50+
// const imageUrl = profile.photos[0]?.value
51+
const returnValue = {
4452
id: profile.id,
45-
username,
46-
name: profile.name.givenName,
47-
imageUrl,
53+
email,
54+
// username,
55+
// name: profile.name,
56+
// imageUrl,
4857
}
58+
return returnValue
4959
},
5060
)
5161
}
@@ -85,21 +95,24 @@ export class GitHubProvider implements AuthProvider {
8595
async handleMockAction(request: Request) {
8696
if (!shouldMock) return
8797

88-
const connectionSession = await connectionSessionStorage.getSession(
89-
request.headers.get('cookie'),
90-
)
9198
const state = cuid()
92-
connectionSession.set('oauth2:state', state)
93-
9499
// allows us to inject a code when running e2e tests,
95100
// but falls back to a pre-defined 🐨 constant
96101
const code =
97102
request.headers.get(MOCK_CODE_GITHUB_HEADER) || MOCK_CODE_GITHUB
98103
const searchParams = new URLSearchParams({ code, state })
104+
let cookie = new SetCookie({
105+
name: 'github',
106+
value: searchParams.toString(),
107+
path: '/',
108+
sameSite: 'Lax',
109+
httpOnly: true,
110+
maxAge: 60 * 10,
111+
secure: process.env.NODE_ENV === 'production' || undefined,
112+
})
99113
throw redirect(`/auth/github/callback?${searchParams}`, {
100114
headers: {
101-
'set-cookie':
102-
await connectionSessionStorage.commitSession(connectionSession),
115+
'Set-Cookie': cookie.toString(),
103116
},
104117
})
105118
}

app/utils/providers/provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Strategy } from 'remix-auth'
1+
import { type Strategy } from 'remix-auth/strategy'
22
import { type Timings } from '../timing.server.ts'
33

44
// Define a user type for cleaner typing

package-lock.json

Lines changed: 53 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@
9999
"react": "^19.0.0",
100100
"react-dom": "^19.0.0",
101101
"react-router": "^7.2.0",
102-
"remix-auth": "^3.7.0",
103-
"remix-auth-github": "^1.7.0",
102+
"remix-auth": "^4.1.0",
103+
"remix-auth-github": "^3.0.2",
104104
"remix-utils": "^8.1.0",
105105
"set-cookie-parser": "^2.7.1",
106106
"sonner": "^1.7.4",

tests/mocks/github.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export async function insertGitHubUser(code?: string | null) {
112112
async function getUser(request: Request) {
113113
const accessToken = request.headers
114114
.get('authorization')
115-
?.slice('token '.length)
115+
?.slice('Bearer '.length)
116116

117117
if (!accessToken) {
118118
return new Response('Unauthorized', { status: 401 })
@@ -145,11 +145,11 @@ export const handlers: Array<HttpHandler> = [
145145
user = await insertGitHubUser(code)
146146
}
147147

148-
return new Response(
149-
new URLSearchParams({
148+
return json(
149+
{
150150
access_token: user.accessToken,
151151
token_type: '__MOCK_TOKEN_TYPE__',
152-
}).toString(),
152+
},
153153
{ headers: { 'content-type': 'application/x-www-form-urlencoded' } },
154154
)
155155
},

0 commit comments

Comments
 (0)