0% found this document useful (0 votes)
34 views3 pages

441073780685538385-Users Controller

This document defines functions for authentication, retrieving user data, sending verification codes via MessageBird, verifying codes, creating new users, and updating users. It requires the jsonwebtoken and messagebird libraries, and connects to a database of users. The functions handle authentication, sending/verifying codes, CRUD operations on the user model, and returning responses.

Uploaded by

Andry Martinez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

441073780685538385-Users Controller

This document defines functions for authentication, retrieving user data, sending verification codes via MessageBird, verifying codes, creating new users, and updating users. It requires the jsonwebtoken and messagebird libraries, and connects to a database of users. The functions handle authentication, sending/verifying codes, CRUD operations on the user model, and returning responses.

Uploaded by

Andry Martinez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

const db = require("../..

/models");
const User = db.users;
const jwt = require('jsonwebtoken')
const messagebird = require('messagebird').initClient('nf9rqbTPfVnFeUIf5q5ooRnSF');

exports.verifyAuthToken = (req, res, next) => {


const bearerHeader = req.headers['authorization']

if (typeof bearerHeader !== 'undefined') {


const authToken = bearerHeader.split(" ")[1]
req.token = authToken
next()
} else {
res.send(403)
}
}

exports.me = (req, res) => {


jwt.verify(req.token, 'secretkey', (err, auth) => {
if (err) {
res.send(403)
}else{
res.json({ auth })
}
})
}

exports.test = (req, res) => {

res.json({ t: 'testing' })

exports.sendCode = (req, res) => {

const phoneNumber = req.body.number

var params = {
'originator': 'TestMessage',
'recipients': [
'RECIPIENT'
],
'body': 'This is a test message'
};

messagebird.verify.create( phoneNumber , {
template: 'Tu codigo de verificación de Caracas Burguer es %token.'
}, function (err, response) {
if (err) {
///Request Failed
console.log('error send code', err.message);
res.render('paso1', {
error: err.message
})
} else {
res.render('codigo enviado', {
id: response.id
})
}
})

exports.verifyCode = (req, res) => {


const id = req.body.id
const phoneNumber = req.body.number
const token = req.body.token

messagebird.verify.verify(id, token, function (err, response) {


if (err) {
res.render('bad token', {
error: err.message,
id: id
})
} else {
User.find({ telefono: phoneNumber })
.then(data => {
////Sesion iniciada con token
res.send(data);
jwt.sign({ data }, 'secretkey', (err, token) => {
res.json({ token })
})
})
.catch(err => {
/// Codigo 404 para mandar al usuario al form de registro de usuario
res.status(404).send({
message:
"User Not Found"
});
});
}
})

// Create and Save a new User


exports.create = (req, res) => {
// Validate request
if (!req.body.nombre) {
res.status(400).send({ message: "Content can not be empty!" });
return;
}

// Create a User
const User = new User({
title: req.body.title,
description: req.body.description,
published: req.body.published ? req.body.published : false
});

// Save User in the database


User
.save(User)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the User."
});
});
};

// Update a User by the id in the request


exports.update = (req, res) => {
if (!req.body) {
return res.status(400).send({
message: "Data to update can not be empty!"
});
}

const id = req.params.id;

User.findByIdAndUpdate(id, req.body, { useFindAndModify: false })


.then(data => {
if (!data) {
res.status(404).send({
message: `Cannot update User with id=${id}. Maybe User was not found!`
});
} else res.send({ message: "User was updated successfully." });
})
.catch(err => {
res.status(500).send({
message: "Error updating User with id=" + id
});
});
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy