Skip to content

Commit

Permalink
Update session auth in the content pages app
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourgreens committed Jun 16, 2023
1 parent 10f936b commit 67b8745
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion packages/app-content-pages/src/shared/stores/User/User.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as panoptesJS from '@zooniverse/panoptes-js'
import asyncStates from '@zooniverse/async-states'
import { applySnapshot, flow, types } from 'mobx-state-tree'
import auth from 'panoptes-client/lib/auth'
Expand All @@ -6,6 +7,42 @@ import numberString from '../types/numberString'

import UserPersonalization from './UserPersonalization'

async function decodeJWT(token) {
let user = null
let error = null
const decodedToken = await panoptesJS.auth.verify(token)
const { data } = decodedToken
error = decodedToken.error
if (data) {
user = {
id: data.id.toString(),
login: data.login,
display_name: data.dname,
admin: data.admin
}
}
return { user, error }
}

async function fetchPanoptesUser() {
try {
const token = await auth.checkBearerToken()
if (token) {
const { user, error } = await decodeJWT(token)
if (user) {
return user
}
if (error) {
throw error
}
}
return await auth.checkCurrent()
} catch (error) {
console.log(error)
return null
}
}

const User = types
.model('User', {
avatar_src: types.maybeNull(types.string),
Expand All @@ -31,7 +68,7 @@ const User = types
checkCurrent: flow(function * checkCurrent () {
self.loadingState = asyncStates.loading
try {
const userResource = yield auth.checkCurrent()
const userResource = yield fetchPanoptesUser()
self.loadingState = asyncStates.success
if (userResource) {
self.set(userResource)
Expand Down

0 comments on commit 67b8745

Please sign in to comment.