Skip to content

Commit

Permalink
auth config
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Jan 25, 2024
1 parent d9acea6 commit dcc8805
Show file tree
Hide file tree
Showing 6 changed files with 1,254 additions and 1,324 deletions.
4 changes: 4 additions & 0 deletions packages/plugins/auth/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"properties": {
"jwtSecret": {
"type": "string"
},
"cookie": {
"type": "object",
"additionalProperties": true
}
},
"required": [
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/auth/src/server/mmorpg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ declare module '@rpgjs/server' {
auth(engine: RpgServerEngine, socket) {
const { globalConfig } = engine
const cookie = socket.request.headers.cookie
const cookieConfig = engine.globalConfig.auth?.cookie
const { jwtSecret } = globalConfig.auth
const token = cookie?.split('rpg-token=')[1]?.split(';')[0]
const token = cookie?.split((cookieConfig?.name || 'rpg-token') + '=')[1]?.split(';')[0]
if (!token) {
throw new Error('No token')
}
Expand Down
8 changes: 7 additions & 1 deletion packages/plugins/auth/src/server/web3/verify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { SiweMessage } from 'siwe'
import { RpgServerEngine, inject } from '@rpgjs/server'
import z from 'zod'
import jwt from 'jsonwebtoken'

Expand All @@ -14,6 +15,8 @@ interface AuthConfig {
export function playerWeb3VerifyHandler(authConfig: AuthConfig) {
return async (req, res, next) => {
try {
const server = inject(RpgServerEngine)

z.object({
message: z.object({
nonce: z.string(),
Expand Down Expand Up @@ -47,11 +50,14 @@ export function playerWeb3VerifyHandler(authConfig: AuthConfig) {
token
}

const cookie = server.globalConfig.auth?.cookie

// http only
res.cookie('rpg-token', token, {
res.cookie(cookie?.name || 'rpg-token', token, {
httpOnly: true,
maxAge: 1000 * 60 * 60 * 24 * 365,
secure: false,
...(cookie || {})
}).send(result)

} catch (error) {
Expand Down
Loading

0 comments on commit dcc8805

Please sign in to comment.