Bcrypt JS issue


#1

I have the following code and I can’t execute it because it doesn’t recognize bcryptjs when I try to dehash the password to see if it matches the user input. The error occurs at “this.result = Bcryptjs.compareSync(data, …”

import { Component } from '@angular/core';
import { ViewController, AlertController } from 'ionic-angular';
import * as Bcryptjs from "bcryptjs";

@Component({
  templateUrl: 'popover.html'
})

export class GodMode {
  
  public hash: string;
  public result: boolean;

  constructor(public viewCtrl: ViewController, public alertCtrl: AlertController) { }

  authentication() {
let prompt = this.alertCtrl.create({
  title: 'Gode Mode Authentication',
  message: "Only the heads of the Scouting Team can access this",
  inputs : [
    {
      name: 'Password',
      type: 'password',
      placeholder: 'Password'
    },
  ],
  buttons: [
    {
      text: 'Cancel',
      handler: data => {
        console.log('Exited Godmode prompt');
        this.viewCtrl.dismiss();
      }
    },
    {
      text: 'Authenticate',
      handler: data =>{
        this.result = Bcryptjs.compareSync(data, "$2a$06$BLaLTSB8AixB.bd4OLNKuOc.CXMS/I27haszcgITbN6niSuAZhv1y");
        console.log(this.result);
      }
    }
  ]
});
prompt.present();
  }

  close() {
this.viewCtrl.dismiss();
  }
}

#2

If you don’t get any better answers, you might try emulating the import syntax from the test file:

import bcryptjs = require("bcryptjs");

#3

I get a error when trying that. It tells me to use import * as Bcryptjs from “bcryptjs”; instead


#4

Something else is going on, then, because the following works for me when I just drop it into the home page of a scratch project:

import * as bcrypt from 'bcryptjs';

bcrypt.genSalt(40).then((salt) => {
      alert(salt);
    });

let compsync = bcrypt.compareSync('apple', 'banana');
alert(compsync);

I get ‘false’ and something that looks reasonable for genSalt()'s output, so bcrypt seems to be working. bcrypt 2.4.3 and types ^2.4.0.


#5

I found the issue it was the input from the form…

Would you happen to know what I have to do to get the input from the form to bcrypt?


#6

Not without seeing enough code that I can copy/paste into my scratchpad.


#7

These are my two .ts files for this page

https://pastebin.com/raw/EC5PsTvB (popup)
https://pastebin.com/raw/PdT48qQe (main)

I put the popup in a separate ts file


#8

Sorry, but that’s not enough to reproduce it.


#9

How much would you need to reproduce it?


#10

Take a new ionic starter project (I tend to use tabs as the template for my scratchpads, but it doesn’t matter much). Isolate enough code from your problem project so that you can copy it into the scratch project and see the problem. Then anybody else can do the same.


#11

Hi Kim,

Did you got solution for this. Please let me know bcryptjs installation process for IONIC3 project


#12

Any Solution for import Bcrypt to ionic v3?? please

I use const bcrypt = require(“bcrypt”);
but have error