-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React Native: How to send auth token in header? #224
Comments
Hello @siddh3sh Will this work for your case - https://github.com/centrifugal/centrifuge-js#custom-websocket-constructor ? |
Please reopen if the approach above did not work or you see a better solution. |
Hello @FZambia , if we use the above approach we are getting below error. |
The link only shows an example how to achieve this. You can pass any WebSocket client wrapper to Centrifuge - so just pass the correct one. What I do not understand though is why you are running it in the browser where passing headers is impossible at all. Sorry - probably I am missing sth. |
hello @FZambia, My project is in React Native and my plan is to use centrifuge with websocket transport. const centrifuge = new Centrifuge([{ |
It seems that React Native has native WebSocket object these days. And quick googling showed that it has third parameter to include headers in non-browser environment: let ws = new WebSocket(url, '', {headers: {'User-Agent': 'ReactNative'}}); So maybe try the following code: const myWs = function (options) {
return class wsClass extends WebSocket {
constructor(...args) {
super(...[...args, ...[options]])
}
}
}
let centrifuge = new Centrifuge('ws://localhost:8000/connection/websocket', {
websocket: myWs({ headers: { Authorization: '<token or key>' } })
}); I.e. without using |
I have tried 2 scenarios, still not worked for me. please go through the below details for clear understanding the issue. let centrifuge = new Centrifuge('ws://localhost:8000/connection/websocket', { |
Let's reopen then, I'll try to check this out soon.. No ready to use React Native environment at the moment. |
@vinodkuumar I tried to start new React Native app:
Using blank Javascript template.
The content of App.js: import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import { Centrifuge } from 'centrifuge';
const myWs = function (options) {
return class wsClass extends WebSocket {
constructor(...args) {
if (args.length === 1) {
super(...[...args, 'centrifuge-json', ...[options]])
} else {
super(...[...args, ...[options]])
}
}
}
}
let centrifuge = new Centrifuge('ws://192.168.1.195:8000/connection/websocket', {
websocket: myWs({ headers: { Authorization: '<token or key>' } })
});
centrifuge.on('connected', function (ctx) {
console.log("connected", ctx);
});
centrifuge.on('connecting', function (ctx) {
console.log("connecting", ctx);
});
centrifuge.on('disconnected', function (ctx) {
console.log("disconnected", ctx);
});
centrifuge.connect();
export default function App() {
return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!!</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
}); It works and properly connects to Centrifugo (please keep in mind that it's just an example, probably you need to bind Centrifuge lifetime to the component's lifetime - don't have experience with React Native at all). In this case, for connection from browser I don't see any Could you please check this out? I was not able to reproduce error messages you had, I had another one though - related to subprotocol argument. |
Hello @FZambia , it worked for me. |
Welcome! Updated readme in this commit - 72723b3. |
For web apps we're setting the JWT in cookie and it's getting sent as header during connection request.
Is there any similar way to pass JWT in React Native applications since we do not have access to cookies here.
The text was updated successfully, but these errors were encountered: