Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
agetic
sitio-firebase
Commits
9f92576e
Commit
9f92576e
authored
Mar 18, 2020
by
Ivan Tancara
Browse files
adicion de error de authorization
parent
9556f740
Changes
1
Hide whitespace changes
Inline
Side-by-side
backend/server.js
View file @
9f92576e
...
...
@@ -28,19 +28,19 @@ app.use(morgan(function (tokens, req, res) {
app
.
use
(
bodyParser
.
json
({
limit
:
'
10mb
'
}));
app
.
use
(
cors
(
corsOptions
));
async
function
mensajeExito
(
res
,
datos
)
{
return
res
.
status
(
200
).
json
({
async
function
mensajeExito
(
res
,
mensaje
=
'
OK
'
,
codigo
=
200
,
datos
)
{
return
res
.
status
(
codigo
).
json
({
finalizado
:
true
,
mensaje
:
'
OK
'
,
datos
:
datos
mensaje
:
mensaje
,
datos
})
}
async
function
mensajeError
(
res
,
mensaje
Error
)
{
return
res
.
status
(
400
).
json
({
async
function
mensajeError
(
res
,
mensaje
=
'
Ocurrio un error desconocido.
'
,
codigo
=
400
,
datos
=
null
)
{
return
res
.
status
(
codigo
).
json
({
finalizado
:
false
,
mensaje
:
mensaje
Error
,
datos
:
null
mensaje
:
mensaje
,
datos
})
}
...
...
@@ -58,7 +58,7 @@ async function AuthorizationMiddleware (req, res, next) {
req
.
usuario
=
decoded
.
usuario
next
();
}
catch
(
error
)
{
mensajeError
(
res
,
error
.
message
);
mensajeError
(
res
,
error
.
message
,
401
);
}
}
...
...
@@ -83,15 +83,18 @@ app.post('/login', async function (req, res) {
if
(
!
existe
)
{
throw
new
Error
(
'
El usuario no existe
'
);
}
mensajeExito
(
res
,
respuesta
);
mensajeExito
(
res
,
'
Login correcto
'
,
200
,
respuesta
);
}
catch
(
error
)
{
mensajeError
(
res
,
error
.
message
);
mensajeError
(
res
,
error
.
message
,
400
);
}
});
app
.
post
(
'
/send
'
,
AuthorizationMiddleware
,
async
function
(
req
,
res
)
{
try
{
const
{
titulo
,
text
}
=
req
.
body
;
if
(
!
titulo
||
!
text
)
{
throw
new
Error
(
'
Debe enviar los parametros necesarios.
'
);
}
const
init
=
{
method
:
'
POST
'
,
url
:
'
https://fcm.googleapis.com/fcm/send
'
,
...
...
@@ -107,11 +110,21 @@ app.post ('/send', AuthorizationMiddleware, async function (req, res) {
badge
:
'
1
'
},
priority
:
'
High
'
}
}
}
const
repsuesta
=
await
axios
(
init
);
// const repsuesta = {};
mensajeExito
(
res
,
repsuesta
.
data
);
// const repsuesta = {
// data: {
// to: '/topics/all',
// notification : {
// body : text,
// title: titulo,
// badge: '1'
// },
// priority: 'High'
// }
// };
mensajeExito
(
res
,
'
Notificación enviada correctamente.
'
,
200
,
repsuesta
.
data
);
}
catch
(
error
)
{
if
(
error
.
response
)
{
console
.
log
(
'
____________________________________________________________________________________________________ERROR_RESPONSE_
'
);
...
...
@@ -122,7 +135,7 @@ app.post ('/send', AuthorizationMiddleware, async function (req, res) {
console
.
log
(
'
___________________________________________________________________________________________________ERROR_MESSAGE_
'
);
console
.
log
(
error
.
message
);
console
.
log
(
'
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ERROR_MESSAGE_
'
);
mensajeError
(
res
,
error
.
message
);
mensajeError
(
res
,
error
.
message
,
401
);
}
}
})
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment