You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
client.auth.signInWithProvider(providerName) - open popup for OAuth provider
client.auth.sendPasswordResetEmail(email)
client.auth.getUserData() - requests /auth/userdata from the server
client.auth.onChange(callback) - define a callback that is triggered when internal auth state changes (it only triggers as a response from client.auth method calls, there's no realtime subscription here!)
client.auth.signOut() - clear local auth token
client.auth.token - auth token getter and setter
Client-side: client.http API:
If client.auth.token is set, requests from client.http will forward it as "Authentication: Bearer {token}" header. The match-making requests are now using client.http as well.
client.http.get(path, options)
client.http.post(path, options)
client.http.del(path, options)
client.http.put(path, options)
Playground update (@colyseus/playground)
The playground tool has been updated to allow customizing the client's "Auth Token".
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Introducing Authentication Module
Documentation | Live demo | Demo Source Code
Server-side:
onAuth()
changesstatic onAuth(token, req)
method should be implemented asstatic
from now ononAuth(client, options, req)
as an instance method still works, but will be deprecated in the future.This change allows validating the token earlier in the connection process, without needing an instance of the room available.
This way the auth token is read from the first matchmaking request header instead of as query param in the second WebSocket connection.
Server-side: The
@colyseus/auth
module/auth/register
- user registration (email + password)/auth/login
- login (email + password)/auth/anonymous
- anonymous login/auth/userdata
- fetch user datagrant
module - a MIT-licensed "OAuth Proxy")/auth/provider/:providerId
- redirect to provider/auth/provider/:providerId/callback
- reply callback from the providerDatabase interaction must be implemented by end-user
End-user should implement the following callbacks:
auth.settings.onFindUserByEmail = async (email) => {/* query user by email */}
auth.settings.onRegisterWithEmailAndPassword = async (email, password, options) => {/* insert user */}
auth.settings.onRegisterAnonymously = async (options: T) => {/* insert anonymous user */}
auth.oauth.onCallback(async (data, provider) => {/* query or insert user by OAuth provider */})
End-user may customize the following callbacks. (They come with a default implementation.)
auth.settings.onParseToken = (jwt: JwtPayload) => jwt
auth.settings.onGenerateToken = async (userdata: unknown) => await JWT.sign(userdata)
auth.settings.onHashPassword = async (password: string) => Hash.make(password)
Server-side Usage
Client-side:
client.auth
API:client.auth.registerWithEmailAndPassword(email, password, options?)
client.auth.signInWithEmailAndPassword(email, password)
client.auth.signInAnonymously(options?)
client.auth.signInWithProvider(providerName)
- open popup for OAuth providerclient.auth.sendPasswordResetEmail(email)
client.auth.getUserData()
- requests/auth/userdata
from the serverclient.auth.onChange(callback)
- define a callback that is triggered when internal auth state changes (it only triggers as a response fromclient.auth
method calls, there's no realtime subscription here!)client.auth.signOut()
- clear local auth tokenclient.auth.token
- auth token getter and setterClient-side:
client.http
API:If
client.auth.token
is set, requests fromclient.http
will forward it as"Authentication: Bearer {token}"
header. The match-making requests are now usingclient.http
as well.client.http.get(path, options)
client.http.post(path, options)
client.http.del(path, options)
client.http.put(path, options)
Playground update (
@colyseus/playground
)The playground tool has been updated to allow customizing the client's "Auth Token".
(PRs introducing the authentication module #657, colyseus/colyseus.js#133, colyseus/docs#150)
Smaller fixes
A big thanks to @afrokick for fixing all these below 👏
This discussion was created from the release 0.15.15.
Beta Was this translation helpful? Give feedback.
All reactions