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);
});
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.?
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
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.
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.
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).