Skip to content

Commit

Permalink
Test Galxe connection in production
Browse files Browse the repository at this point in the history
As the OAuth callback from Galxe'S side is restricted to the registered prodution URL.
  • Loading branch information
sisou committed Oct 8, 2024
1 parent b3df738 commit 47e9328
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 36 deletions.
44 changes: 31 additions & 13 deletions app/components/Card/Galxe.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ const data = {
const x = ref(0)
const galxeEnabled = ref(false)
onMounted(() => {
setTimeout(() => {
// Get user leaderboard % position from Galxe api
x.value = 51
}, 500)
// @ts-expect-error enableGalxe is not a property on window
window.enableGalxe = () => {
galxeEnabled.value = true
}
})
const mr = computed(() => {
return (x.value / 100) * 49
Expand All @@ -54,19 +61,30 @@ const mr = computed(() => {
<div v-if="!store.hasClaimed" class="i-custom:lock-outline absolute left-1/2 top-0 text-40 -translate-1/2" title="Claim your points to unlock this card" />

<!-- Icon -->
<div class="icon-shadow mx-auto mb-32 w-fit object-contain object-center">
<div class="i-custom:galxe h-28 w-144" />
</div>
<div class="small-body text-center text-white/60">
Share the news with Galxe to multiply your points. Coming soon!
</div>
<button v-if="true || !store.hasClaimed" disabled class="mx-auto mt-24 cursor-pointer nq-pill-secondary">
Connect
</button>
<a v-else-if="!store.galxeId" href="/api/galxe/connect" class="mx-auto mt-24 cursor-pointer nq-pill-secondary">Connect</a>
<p v-else class="mt-16 text-center text-14 text-white">
Galxe points are coming soon, check back in a few days!
</p>
<template v-if="!store.galxeUser">
<div class="icon-shadow mx-auto mb-32 w-fit object-contain object-center">
<div class="i-custom:galxe h-28 w-144" />
</div>
<div class="small-body text-center text-white/60">
Share the news with Galxe to multiply your points. Coming soon!
</div>
<button v-if="!galxeEnabled" disabled class="mx-auto mt-24 cursor-pointer nq-pill-secondary">
Connect
</button>
<a v-else href="/api/galxe/connect" class="mx-auto mt-24 cursor-pointer nq-pill-secondary">Connect</a>
</template>
<template v-else>
<div class="icon-shadow mx-auto mb-32 w-fit object-contain object-center">
<img :src="store.galxeUser.Avatar" class="h-64 w-64">
</div>
<div class="small-body text-center text-white">
{{ store.galxeUser.Name }}
</div>
<p class="mt-16 text-center text-14 text-white/60">
Galxe points are coming soon, check back in a few days!
</p>
</template>

<RewardMultiplierBadges
:multipliers="galxeRewardData.options"
:active="[]"
Expand Down
6 changes: 5 additions & 1 deletion app/components/Nimiq/LoginModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ async function acceptTC() {
throw err
})
await useUserInfo().updateStats(user.id, user.address) // TODO: Error handling
await useUserInfo().updateStats({
id: user.id,
address: user.address,
galxeUser: user.galxeUser,
})
emit('close')
}
</script>
Expand Down
31 changes: 17 additions & 14 deletions app/stores/userInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ export const useUserInfo = defineStore('userInfo', {
state: () => ({
userId: null as string | null,
address: null as string | null,
galxeUser: null as {
Avatar: string // URL
GalxeID: string
Name: string
} | null,

stake: 0,
totalPoints: 0,
galxeId: null as string | null,
hasClaimed: false,

basePoints: 0,
Expand All @@ -31,34 +36,31 @@ export const useUserInfo = defineStore('userInfo', {
}),
actions: {
async tryFetch() {
const user = await $fetch('/api/address').catch(() => null)
const user = await $fetch('/api/auth/user').catch(() => null)
if (!user)
return false

await this.updateStats(user.id, user.address)
await this.updateStats(user)
return true
},
async updateStats(id: string, address: string) {
async updateStats(user: { id: string, address: string, galxeUser: { Avatar: string, GalxeID: string, Name: string } | null }) {
const stats = await $fetch('/api/update-stats', {
method: 'POST',
}).catch(() => null)

if (!stats) {
useUserInfo().$patch({
userId: id,
address,
})
useUserInfo().$patch(user)
return
}

useUserInfo().$patch({
userId: id,
address,
userId: user.id,
address: user.address,
galxeUser: user.galxeUser || null,

hasClaimed: stats.hasClaimed,
stake: stats.stake,
totalPoints: stats.totalPoints,
galxeId: stats.galxeId || null,
hasClaimed: stats.hasClaimed,

basePoints: stats.basePoints,
earlyBirdPoints: stats.earlyBirdPoints,
underdogPoints: stats.underdogPoints,
Expand All @@ -72,9 +74,10 @@ export const useUserInfo = defineStore('userInfo', {
logout() {
this.userId = null
this.address = null
this.galxeUser = null

this.stake = 0
this.totalPoints = 0
this.galxeId = null
this.hasClaimed = false

this.basePoints = 0
Expand Down
7 changes: 5 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ export default defineNuxtConfig({
},

runtimeConfig: {
galxeClientId: '', // Replaced by NUXT_GALXE_CLIENT_ID
galxeClientSecret: '', // Replaced by NUXT_GALXE_CLIENT_SECRET
// eslint-disable-next-line node/prefer-global/process
galxeClientId: process.env.NUXT_GALXE_CLIENT_ID, // Replaced by NUXT_GALXE_CLIENT_ID
// eslint-disable-next-line node/prefer-global/process
galxeClientSecret: process.env.NUXT_GALXE_CLIENT_SECRET, // Replaced by NUXT_GALXE_CLIENT_SECRET
public: {
validatorsApiUrl: 'https://validators-api.pages.dev/api/v1', // Replaced by NUXT_PUBLIC_VALIDATORS_API_URL
albatrossLiveviewUrl: 'https://nimiq-website-nimiq.nuxt.dev/iframes/albatross-liveview', // Replaced by NUXT_PUBLIC_ALBATROSS_LIVEVIEW_URL
Expand Down Expand Up @@ -64,6 +66,7 @@ export default defineNuxtConfig({
contentSecurityPolicy: {
'frame-src': ['\'self\''],
'frame-ancestors': ['\'self\''],
'img-src': false,
},
xFrameOptions: 'SAMEORIGIN',
crossOriginEmbedderPolicy: 'unsafe-none',
Expand Down
1 change: 1 addition & 0 deletions server/api/auth/login.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default defineEventHandler(async (event): Promise<User> => {
delegation: null,
hasClaimed: false,
totalPoints: 0,
galxeUser: null,
createdAt: new Date().toJSON(),
updatedAt: new Date().toJSON(),
}
Expand Down
1 change: 1 addition & 0 deletions server/api/address.get.ts → server/api/auth/user.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export default defineEventHandler(async (event) => {
return {
id: user.id,
address: user.address,
galxeUser: user.galxeUser,
}
})
8 changes: 4 additions & 4 deletions server/api/galxe/callback.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export default defineEventHandler(async (event) => {
}

const galxeUser = await $fetch<{
GalxeUserID: string
Avatar: string // URL
GalxeID: string
Name: string
}>('https://api.galxe.com/oauth/api/2/user?scope=GalxeID', {
headers: {
Authorization: `${tokens.token_type} ${tokens.access_token}`,
Expand All @@ -55,9 +57,7 @@ export default defineEventHandler(async (event) => {
throw notAcceptableError(`Failed to fetch Galxe user: ${galxeUser.message}`)
}

console.log({ galxeUser }) // eslint-disable-line no-console
user.galxeId = galxeUser.GalxeUserID

user.galxeUser = galxeUser
await userDb.set(user.id, user)

return sendRedirect(event, '/pre-staking')
Expand Down
1 change: 0 additions & 1 deletion server/api/update-stats.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ export default defineEventHandler(async (event) => {
delegation: user.delegation,
hasClaimed: user.hasClaimed,
totalPoints: user.totalPoints,
galxeId: user.galxeId,
}
})
6 changes: 5 additions & 1 deletion server/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export interface User {
delegation: Address | null
hasClaimed: boolean
totalPoints: number
galxeId?: string
galxeUser: {
Avatar: string // URL
GalxeID: string
Name: string
} | null
createdAt: string
updatedAt: string
}
Expand Down

0 comments on commit 47e9328

Please sign in to comment.