Answer the question
In order to leave comments, you need to log in
Why is the token not generated?
Services:
addUser = async (body) => {
const user = new User(body);
await user.save();
if (!(await fs.pathExists(`public/images/users/${user._id}`))) {
await fs.ensureDir(`public/images/users/${user._id}`);
}
const token = await user.generateAuthToken();
console.log(token)
return { ...user, token };
};
const userSchema = new mongoose.Schema({
login: {
type: String,
unique: true,
required: true,
trim: true
},
firstName: {
type: String
},
lastName: {
type: String
},
email: {
type: String
},
password: {
type: String,
required: true,
minlength: 3,
trim: true,
validate(value) {
if (value.toLowerCase().includes('password')){
throw new Error('Password cannot contain "password"');
}
}
},
avatar: {
type: String
},
role: {
type: String,
default: 'user'
},
tokens: [
{
token: {
type: String,
required: true
}
}
]
});
userSchema.methods.generateAuthToken = async function() {
const user = this;
const token = jwt.sign({ _id: user._id.toString() }, process.env.KeyWord);
user.tokens = await user.tokens.concat({ token });
user.save();
return token;
};
const User = mongoose.model("Users", userSchema);
Answer the question
In order to leave comments, you need to log in
class DB
{
private static $db;
private $host = 'localhost';
private $dbname = 'test';
private $charset = 'utf8';
private $username = 'root';
private $password = '';
static public function connect()
{
if (self::$db === Null) {
self::$db = new self();
}
return self::$db;
}
private function __clone() {}
private function __construct()
{
return new \PDO("mysql:host=".$this->host.";dbname=".$this->dbname.";charset=".$this->charset, $this->username, $this->password);
}
}
$db = DB::connect();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question