Ionic-2 authentiaction with random string(as a token)


#1

Hello…

I am new to Ionic2 and working on user authentication task. For this, i have to generate a random string and take that string as a token. Any one have an idea how to generate a random string with ionic2 and how to authenticate a user using that string as a token.

I have do like this:

login(): void{

this.nav.present(this.loading);
this.dataService.postData('bookappointment/login', this.loginForm.value).then((res) => {
  this.loading.dismiss(); 
  if(res.error){
    let alert = this.simpleAlert.createAlert('Oops!', 'Invalid Credentials!');
    this.nav.present(alert);
  }else{ 
    this.generateToken();
    this.nav.setRoot(IndexPage);
    console.log(this.loginForm.value);
  }
  
}, (err) => {
  this.loading.dismiss();
  let alert = this.simpleAlert.createAlert('Oops!', 'Something went wrong, please try again later.');
  this.nav.present(alert);
}); 

}


#2

@hiren004 I suggests you some authentication library… that will be much more easier
Take a look on ng2-ui-auth There is also an ionic 2 example


#3

Thanks for your reply…but i need to do authentication with generating a random string and take it as a token.:slight_smile:


#4

Doesnt JWT token will do the work? https://jwt.io/


#5

I am not trying with that right now. The code i have posted, is working fine with authentication of a user. I just need to generate a random string. Do you have any idea how to generate a random string this.?:slight_smile:


#6

Maybe UUID? https://www.npmjs.com/package/angular2-uuid


#7

Ok…you mean after installing it, then i can use math.random()??


#8

Math.random() is a build in function… you can use it where you like… but this will give you a random number…
since you wanted a random string for me UUID seams like the best option…

Just install the package import it and then you will be able to use it like that:
import { UUID } from 'angular2-uuid';
let uuid = UUID.UUID();

uuid will look like: 547d8e40-354c-11e6-aa97-083e8ec535fb


#9

Ok…thanks for your best suggestion…:slight_smile:, After installing package, is using let uuid = UUID.UUID(); generates random string? No need to do anyhing?


#10

import { UUID } from 'angular2-uuid'; Just dont forget to import


#11

Ok…thank a lot:slight_smile:


#12

I had a look at the implementation of angular2-uuid, and it seems very naive and not nearly as robust as angular-uuid which is a wrapper around node-uuid, a RFC4122 UUID generator. Would you recommend using angular2-uuid?


#13

@nieldw No, it’s dangerous because of this issue. Which is a shame because importing node-uuid into Ionic 2 is a pain. But importing node-uuid or writing your own conformant implementation of the UUID algorithm seem like the best options right now.


#14

?

import uuid from 'node-uuid';

let foo = uuid.v4();

That works just fine for me, and I think it did so using either webpack or rollup.


#15

Thanks for the response @rapropos. I’ve appreciated your comments about this here and GitHub. But may I ask: with RC3 is it now easier to import uuid than it was when you wrote this comment? I played around with what you suggested and got a variety of fun errors.


#16

With ionic-app-scripts >= 0.0.39 (IIRC), webpack is the default bundler, and as long as you have:

  externals: {
    "crypto": "crypto"
  },

in your webpack configuration, it should work fine.


#17

I appreciate your time @rapropos, but your answer was a bit opaque for my level of webpack understanding. I ended up writing an ID generation service that is robust enough for alpha, and I’ll make it fully conformant later, either by learning webpack, or by upgrading my algorithm implementation.


#18

For anyone else struggling with this, I got it working by removing mention of the uuid package from app.modules.ts. With that gone, the instructions from @rapropos worked. More specifically, placing the externals node in projectName/node_modules/@ionic/app-scripts/config/webpack.config.js. Then import uuid from 'uuid' in the ts file I want to call uuid from.(importing from ‘node-uuid’ worked also, but it seems not as current if I am reading things correctly).

Thanks again.


#19

And for anybody else reading this, UNDER NO CIRCUMSTANCES DO WHAT THE OP IS SUGGESTING".

Cryptography is incredibly hard. Use peer-reviewed protocols. Do not roll your own.