Skip to content

Commit d386260

Browse files
authored
Merge pull request #339 from developmentseed/feature/auth-mapping-team
Moves the authentication to use auth.mapping.team
2 parents 002354f + 9bc6196 commit d386260

File tree

8 files changed

+55
-30
lines changed

8 files changed

+55
-30
lines changed

cypress/e2e/auth.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Check protected routes', () => {
2121
protectedRoutes.forEach((testRoute) => {
2222
it(`Route ${testRoute} needs authentication`, () => {
2323
cy.visit(testRoute)
24-
cy.get('body').should('contain', 'Sign in with openstreetmap')
24+
cy.get('body').should('contain', 'Sign in with OSM Teams')
2525
})
2626
})
2727

cypress/support/commands/login.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
const getSessionToken = require('../../../tests/utils/get-session-token')
22

3-
Cypress.Commands.add('login', (userObj) => {
3+
Cypress.Commands.add('login', (user) => {
44
// Generate and set a valid cookie from the fixture that next-auth can decrypt
55
cy.wrap(null)
66
.then(() => {
7-
return getSessionToken(
8-
{ ...userObj, sub: userObj.id },
9-
Cypress.env('NEXTAUTH_SECRET')
10-
)
7+
return getSessionToken(user, Cypress.env('NEXTAUTH_SECRET'))
118
})
129
.then((encryptedToken) =>
1310
cy.setCookie('next-auth.session-token', encryptedToken)

src/components/sidebar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export default function Sidebar() {
285285
) : (
286286
<Button
287287
className='global-menu__link login'
288-
onClick={() => signIn('openstreetmap')}
288+
onClick={() => signIn('osm-teams')}
289289
>
290290
Sign in
291291
</Button>
Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,64 @@
11
import NextAuth from 'next-auth'
2+
import { mergeDeepRight } from 'ramda'
3+
const db = require('../../../lib/db')
24

35
export const authOptions = {
6+
// Configure one or more authentication providers
47
providers: [
58
{
6-
id: 'openstreetmap',
7-
name: 'openstreetmap',
8-
clientId: process.env.OSM_CONSUMER_KEY,
9-
clientSecret: process.env.OSM_CONSUMER_SECRET,
9+
id: 'osm-teams',
10+
name: 'OSM Teams',
1011
type: 'oauth',
11-
authorization: {
12-
url: 'https://www.openstreetmap.org/oauth2/authorize',
13-
params: {
14-
scope: 'read_prefs',
15-
},
16-
},
17-
token: 'https://www.openstreetmap.org/oauth2/token',
18-
userinfo: 'https://api.openstreetmap.org/api/0.6/user/details.json',
19-
profile({ user }) {
12+
wellKnown: 'https://mapping.team/hyauth/.well-known/openid-configuration',
13+
authorization: { params: { scope: 'openid offline' } },
14+
idToken: true,
15+
async profile(profile) {
2016
return {
21-
id: user.id,
22-
name: user.display_name,
23-
image: user.img?.href,
17+
id: profile.sub,
18+
name: profile.preferred_username,
19+
image: profile.picture,
2420
}
2521
},
22+
clientId: process.env.OSM_TEAMS_CLIENT_ID,
23+
clientSecret: process.env.OSM_TEAMS_CLIENT_SECRET,
2624
},
2725
],
2826
callbacks: {
27+
async jwt({ token, account, profile }) {
28+
// Persist the OAuth access_token and or the user id to the token right after signin
29+
if (account) {
30+
token.accessToken = account.access_token
31+
token.userId = profile.sub
32+
}
33+
return token
34+
},
2935
async session({ session, token }) {
30-
// Add user id to session
31-
const userId = parseInt(token.sub)
32-
session.user_id = userId
36+
// Send properties to the client, like an access_token and user id from a provider.
37+
session.accessToken = token.accessToken
38+
session.user_id = token.userId
3339
return session
3440
},
3541
},
42+
43+
events: {
44+
async signIn({ profile }) {
45+
// On successful sign in we should persist the user to the database
46+
let [user] = await db('users').where('id', profile.id)
47+
if (user) {
48+
const newProfile = mergeDeepRight(user.profile, profile)
49+
await db('users')
50+
.where('id', profile.id)
51+
.update({
52+
profile: JSON.stringify(newProfile),
53+
})
54+
} else {
55+
await db('users').insert({
56+
id: profile.id,
57+
profile: JSON.stringify(profile),
58+
})
59+
}
60+
},
61+
},
3662
}
3763

3864
export default NextAuth(authOptions)

src/pages/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export default function Home() {
207207
</Button>
208208
</div>
209209
) : (
210-
<Button onClick={() => signIn('openstreetmap')}>Sign in →</Button>
210+
<Button onClick={() => signIn('osm-teams')}>Sign in →</Button>
211211
)}
212212
</div>
213213
<div className='map-bg' />

src/pages/teams/[id]/invitations/[invitationId].js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function TeamInvitationPage({ errorCode, errorMessage }) {
2121
<article className='inner page'>
2222
<section>
2323
<h1>Please sign in</h1>
24-
<Button onClick={() => signIn('openstreetmap')}>Sign in →</Button>
24+
<Button onClick={() => signIn('osm-teams')}>Sign in →</Button>
2525
</section>
2626
</article>
2727
)

tests/utils/create-agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function createAgent(user) {
55

66
if (user) {
77
const encryptedToken = await getSessionToken(
8-
{ ...user, sub: user.id },
8+
user,
99
process.env.NEXTAUTH_SECRET
1010
)
1111
agent.set('Cookie', [`next-auth.session-token=${encryptedToken}`])

tests/utils/get-session-token.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { hkdf } = require('@panva/hkdf')
22
const { EncryptJWT } = require('jose')
33

4-
async function getSessionToken(token, secret) {
4+
async function getSessionToken(userObj, secret) {
5+
const token = { ...userObj, userId: userObj.id }
6+
57
// Function logic derived from https://github.com/nextauthjs/next-auth/blob/5c1826a8d1f8d8c2d26959d12375704b0a693bfc/packages/next-auth/src/jwt/index.ts#L113-L121
68
const encryptionSecret = await await hkdf(
79
'sha256',

0 commit comments

Comments
 (0)