heroiclabs / nakama

Distributed server for social and realtime games and apps.
https://heroiclabs.com
Apache License 2.0
9.01k stars 1.1k forks source link

jsonwebtoken(jwt) add module #952

Closed Mehrdadgame closed 1 year ago

Mehrdadgame commented 1 year ago

Hi I working with Nakama typescript I want use jsonwebtoken for login custom but i have error in Tsconfig.json and change "module": "System" in tsconfig and add let jws= require('jsonwebtoken'); error on start server . Failed initializing runtime modules","error":"ReferenceError: require is not defined at index.js:84:18(54)

Description

Tsconfig.json :

"compilerOptions": { "target": "ES5", "module": "System", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "outFile": "./modules/index.js", "typeRoots": [ "./node_modules" , "./node_modules/@types" ] }

Expected Result

const verifyAndParseJwt = function (secretKey: string, jwtt: string , logger:nkruntime.Logger): Claims { let claims: Claims={id:"", username:""}; // Use your favourite JWT library to verify the signature and decode the JWT contents

jws.sign({ foo: jwtt }, secretKey, { algorithm: 'RS256' }, function(err:any, token:any) { logger.info(token + " %%%%%%%%%%%%%%%%%%%%%%"); claims.id=token.id; claims.username = token.username; });

Context

Mehrdadgame commented 1 year ago

sss bbb

novabyte commented 1 year ago

@Mehrdadgame The JavaScript runtime in the game server is not a Node runtime; it is also sandboxed for performance and security reasons. We already provide a jwtGenerate function in the server framework you can use to generate the token structure you need:

https://heroiclabs.com/docs/nakama/server-framework/typescript-runtime/function-reference/#jwtGenerate

Hope this helps.