2

I'm working on a React Native project and I need to implement authentication, with an encrypted password.

I want to use library Bcrypt but I have an issue. Bcrypt requires the Crypto module, which was a third-part library, but now it is a built-in module in Node.js.

When I try to use Bcrypt with const bcrypt = require('bcrypt'), it throws an error :

Unable to resolve module 'crypto' from 'node_modules/bcrypt/...'

I am using :

  • Node.js v13.0.1
  • NPM v6.12.0
  • react-native-cli v2.0.1
  • react-native v0.61.3
  • react v16.9.0

To manage this error, I went to Node.js documentation and tried the solution to know if the Crypto built-in module is available or not but it throws the same error.

Maybe I don't understand clearly what is a built-in module, is that a module which is included into the Node.js installer, so I don't have any NPM install crypto to do?

If it is the case, does anyone have an idea about why I can't use it in my project?

I also tried the rn-nodeify module, with react-native-bcrypt and react-native-crypto, but in each case some packages weren't reached, like stream, VM, fs, etc.

Do you encountered the same problem and solved it?

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
  • Why do you want to crypt the password in your mobile app? it should be done I think in your back-end code it will be more helpful and reduce authentication process time – Rebai Ahmed Nov 07 '19 at 14:18
  • Well, the way I will use it is not defined precisely. What I would like to do is encrypt the password that is submitted and then compare with the one in my database, only if the username is in the database. What you recommend is to encrypt the password during the fetch post method ? I am new to React Native, but if I am not wrong, in my project, my back-end is composed by web services / web API which search the results or post the requests when I use a fetch method in my front end component. Is that rude to use fetch method in the front-end ? – Ragnar's coding Nov 08 '19 at 07:18
  • Do you use nodejs express for your back-end ? – Rebai Ahmed Nov 11 '19 at 08:28
  • No, express is not used in this project. Do you think it might be the problem ? – Ragnar's coding Dec 16 '19 at 08:15
  • Ok, I think I figured out why it is not working. I am not in a node project but I am in a RN project, so packages are not the same. What an idiot I am ! – Ragnar's coding Jan 14 '20 at 13:49

3 Answers3

1

Someone already answered that question here Bcrypt is not supported in Reactjs It's because bcrypt is written in C++. But now they have bcryptjs, the JS version of bcrypt. You can download it here https://www.npmjs.com/package/bcryptjs. Note that bcryptjs is slower than the C++ version - bcrypt, because "it is written in pure JavaScript and thus slower (about 30%), effectively reducing the number of iterations that can be processed in an equal time span."

0

I would request you to look into rn-nodeify library. It enables you to use core modules and npm modules in your react-native app.

Look into this answer rn-nodeify installation and start from Step 2.

I hope i could help you.

Ron Astle Lobo
  • 1,284
  • 2
  • 14
  • 34
  • I already tried with this package following the tutorial installation provided here [rn-nodeify](https://www.npmjs.com/package/rn-nodeify) and the results weren't great. But I'll try the link you sent me, in the case of. I'll make you a return. Thanks ! – Ragnar's coding Nov 08 '19 at 07:21
0

https://github.com/margelo/react-native-quick-crypto I've used this lib the fix for endian.h is to rename it to <machine/endian.h>

Danila Plee
  • 698
  • 5
  • 5