Replies: 2 comments 2 replies
-
Hey @un-versed! 👋🏻 For the moment, you cannot use the |
Beta Was this translation helpful? Give feedback.
-
Hi @un-versed! I resolve this problem with sending user's credentials in query/auth, for example: Client: this.socket = io('http://localhost:3333', {
transports: ['polling', 'websocket'],
auth: {
email: 'johndoe@mail.com',
password: 'yourPass',
},
}).connect() Server: class Ws {
public io: Server
private booted = false
public boot () {
if (this.booted) {
return
}
this.booted = true
this.io = new Server(AdonisServer.instance!, {
cors: {
origin: '*',
},
allowEIO3: true,
})
this.io.use(async (socket, next) => {
const { email, password } = socket.handshake.auth
const authorized = await this.check(email, password)
// Check if account exists and is valid
if (authorized) {
return false
}
next()
})
}
private async check (email, password) {
const user = await User.findByOrFail('email', email)
return !(await Hash.verify(user.password, password))
}
}
export default new Ws() Don't worry about credentials being passed, connecting with SSL/HTTPs will not make this information visible. I used with reference the documentation in here - https://docs.adonisjs.com/cookbooks/socketio-with-adonisjs Take care! |
Beta Was this translation helpful? Give feedback.
-
It's possible to check an api token outside of a HTTP request?
I'm trying to implement socket.io server in my existing adonis application, but I didn't figure out how to authenticate the socket.io connection.
I already have the token input, I just need to verify it
Any tips on this?
Beta Was this translation helpful? Give feedback.
All reactions