2016-06-08 90 views
-1

我試圖捕捉用戶輸入憑證,並用它們作爲參數來查詢數據庫。不幸的是,我對如何編寫這個過程有點遺憾。我正在使用angular,express,node,jQuery和html。我對角度,節點和jQuery不太瞭解,所以如果這很簡單,請原諒我。我在這裏學習。如何從HTTP POST獲取PARAMS和插入

這裏就是形式住的HTML:

<!DOCTYPE html > 
<html ng-app="token"> 
<%include header%> 
<%include navbar%> 
<div ng-controller="TokenCtrl"> 
<form ng-submit="submitLogin(loginForm)" role="form" ng-init="loginForm = {}"> 
<div class="form-group"> 
<label>email</label> 
<input type="email" name="email" ng-model="loginForm.email" required="required" class="form-control"/> 
</div> 
<div class="form-group"> 
<label>password</label> 
<input type="password" name="password" ng-model="loginForm.password" required="required" class="form-control"/> 
</div> 
<button class="btn btn-primary btn-lg" ng-click="handleLoginBtnClick()">Sign in</button> 
</form> 
</div> 
</body> 

這裏是JS爲TokenCtrl和令牌模塊,它是NG-令牌-AUTH的衍生物:

var a = angular.module('token', ['ng-token-auth']); 
a.config(function($authProvider) { 
// the following shows the default values. values passed to this method 
// will extend the defaults using angular.extend 

$authProvider.configure({ 
    apiUrl:     '/users', 
    tokenValidationPath:  '/auth/validate_token', 
    signOutUrl:    '/auth/sign_out', 
    emailRegistrationPath: '/auth', 
    accountUpdatePath:  '/auth', 
    accountDeletePath:  '/auth', 
    confirmationSuccessUrl: window.location.href, 
    passwordResetPath:  '/auth/password', 
    passwordUpdatePath:  '/auth/password', 
    passwordResetSuccessUrl: window.location.href, 
    emailSignInPath:   '/auth/sign_in/:email/:password', 
    storage:     'cookies', 
    forceValidateToken:  false, 
    validateOnPageLoad:  true, 
    proxyIf:     function() { return false; }, 
    proxyUrl:    '/proxy', 
    omniauthWindowType:  'sameWindow', 
    tokenFormat: { 
    "access-token": "{{ token }}", 
    "token-type": "Bearer", 
    "client":  "{{ clientId }}", 
    "expiry":  "{{ expiry }}", 
    "uid":   "{{ uid }}" 
    }, 
    cookieOps: { 
    path: "/", 
    expires: 9999, 
    expirationUnit: 'days', 
    secure: false, 
    domain: 'domain.com' 
    }, 
    createPopup: function(url) { 
    return window.open(url, '_blank', 'closebuttoncaption=Cancel'); 
    }, 
    parseExpiry: function(headers) { 
    // convert from UTC ruby (seconds) to UTC js (milliseconds) 
    return (parseInt(headers['expiry']) * 1000) || null; 
    }, 
    handleLoginResponse: function(response) { 
    return response.data; 
    }, 
    handleAccountUpdateResponse: function(response) { 
    return response.data; 
    }, 
    handleTokenValidationResponse: function(response) { 
    return response.data; 
    } 
}); 
}); 
    a.controller('TokenCtrl', function($scope, $auth) { 
    $scope.handleRegBtnClick = function() { 
    $auth.submitRegistration($scope.registrationForm) 
    .then(function(resp) { 
     // handle success response 
    }) 
    .catch(function(resp) { 
     // handle error response 
    }); 
}; 
$scope.handlePwdResetBtnClick = function() { 
    $auth.requestPasswordReset($scope.pwdResetForm) 
    .then(function(resp) { 
     // handle success response 
    }) 
    .catch(function(resp) { 
     // handle error response 
    }); 
}; 
$scope.handleLoginBtnClick = function() { 
    $auth.submitLogin($scope.loginForm) 
    .then(function(resp) { 
     // handle success response 
    }) 
    .catch(function(resp) { 
     // handle error response 
    }); 
}; 
$scope.handleSignOutBtnClick = function() { 
    $auth.signOut() 
    .then(function(resp) { 
     // handle success response 
    }) 
    .catch(function(resp) { 
     // handle error response 
    }); 
}; 
}); 

在運行這個函數,導致這個url:

'/auth/sign_in/:email/:password' 

使用Express,我把這個url路由到另一個函數。這裏是路線代碼:

app.post('/users/auth/sign_in/:email/:password', routes.verifyusers); 

導致,

exports.verifyusers= function(req, res) { 
models.user.find({ 
where: { 
    email: req.params.email, 
    password: req.params.password 
} 
}).then(function(user) { 
    if(user) { 
     console.log("alright !") 
    }; 
}); 
}; 

當代碼運行,這是我在控制檯中:

Executing (default): SELECT "id", "username", "email", "password", "createdAt", "updatedAt" FROM "users" AS "user" WHERE "user"."email" = ':email' AND "user"."password" = ':password' LIMIT 1; 
:email 
:password 

這是結果是不管到表單數據。

+1

什麼問題,你有與您發佈的代碼?我不知道你的問題是什麼......作爲一個附註,你應該在將密碼發送到數據庫之前對其進行哈希處理。 – user3

+0

請參閱更新,對不起。當我運行代碼時,我沒有捕獲和插入表單數據 - 我只得到基於url的結果,這是:email和:密碼 –

+0

我不知道要$ auth.submitLogin在做什麼,但它似乎像「/ users/auth/sign_in /:email /:password」那樣精確地調用url。 – user3

回答

0

我認爲這個問題是從emailSignInPath: '/auth/sign_in/:email/:password',

來你應該

// config 
emailSignInPath: '/auth/sign_in' 

// route declaration 
app.post('/users/auth/sign_in', routes.verifyusers); 

// route action 
exports.verifyusers = function(req, res) { 
    models.user.find({ 
    where: { 
     email: req.body.email, 
     password: req.body.password 
    } 
    }).then(function(user) { 
    if(user) { 
     console.log("alright !") 
    }; 
    }); 
}; 

PS嘗試:不要忘了聲明主體解析器在您的應用程序app.use(express.bodyParser())

+0

這樣的結果的URL –

+0

我編輯提到bodyparser你有一箇中間件嗎? – user3

+0

是的,我做過。雖然,我認爲這裏的問題是沒有參數的數據,我們需要從表單中獲取數據。 –