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


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.dataService.postData('bookappointment/login', this.loginForm.value).then((res) => {
    let alert = this.simpleAlert.createAlert('Oops!', 'Invalid Credentials!');
}, (err) => {
  let alert = this.simpleAlert.createAlert('Oops!', 'Something went wrong, please try again later.');


@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

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

Doesnt JWT token will do the work?

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:

Maybe UUID?

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

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

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?

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

Ok…thank a lot:slight_smile:

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?

@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.


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.

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.

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.

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.

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.

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.

1 Like