Is there any way to know if the user is logged in using knockout js? How can I get the user's data? I tried this code,
var userId = '<%=HttpContext.Current.Session["user_session"] %>';
alert(userId);
I put it in a view model. But it is not working.
Here is the requested PHP code,
public function login($email,$pass)
{
try
{
$stmt = $this->db->prepare("SELECT * FROM accounts WHERE emailadd=:email LIMIT 1");
$stmt->execute(array(':email'=>$email));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
if(password_verify($pass, $userRow['password']))
{
$_SESSION['user_session'] = $userRow['id'];
return true;
}
else
{
return false;
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
And here is the js code,
function homeViewModel(params) {
var self = this;
var userId = '<%=HttpContext.Current.Session["user_session"] %>';
alert(userId);
return self;
}