Skip to content

Instantly share code, notes, and snippets.

@olkunmustafa
Created August 29, 2016 14:28
Show Gist options
  • Save olkunmustafa/6b07aed841d30600b9b571b7b3bfdb90 to your computer and use it in GitHub Desktop.
Save olkunmustafa/6b07aed841d30600b9b571b7b3bfdb90 to your computer and use it in GitHub Desktop.
// 9001 : Tüm alanlar doldurulmalı mesjaı verir.
// 9002 : Bu mail adresine ait bir kullanıcı zaten var.
// 9003 : Kayıt yapıldı ve değerler geri dönüldü.
var userName, mailAddress, password;
module.exports = {
post : function(request, response, next) {
userName = request.body.userName;
mailAddress = request.body.mailAddress;
password = request.body.password;
if( userName === undefined || mailAddress === undefined || password === undefined ){
sendResult( response, 9001, "Please fill the neccessary fields!" );
} else {
request.azureMobile.tables('Users') // Büyük küçük har uyumuna dikkat
.where({ userMailAddress: mailAddress })
.read()
.then( results => onResult( response, request, next, results ) )
.catch( next ); // it is important to catch any errors and log them
}
}
};
function onResult(response, request, next, results) {
if( results.length == 0 ){
var item = {
userMailAddress: mailAddress,
userNameSurname: userName,
userPassword: password,
authToken : token(),
refreshToken : token(),
tokenExpire : 1440 * 2
};
request.azureMobile.tables( 'Users' )
.insert(item)
.then( insertResults => sendResult( response, 9003, JSON.stringify( insertResults ) ) )
.catch(next);
} else {
sendResult( response, 9002, "There is exist!" );
}
}
function sendResult( response, status, value ) {
var result = "{ " +
"status: " + status +
", result: " + value +
" }";
response.send( 200, result );
}
function token() {
return "Basic " + rand() + rand(); // to make it longer
};
function rand() {
return Math.random().toString(36).substr(2); // remove `0.`
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment