Reputation: 71
I have tried many versions and just don't know the answer. I have looked at the documentation but don't know enough to get it right. I am trying to have a user login to website using parse. i can get the user to login but, can't keep the current user data. As soon as you go back the user has to login again. I know you can use current user for this but i just can't get it to work.
$(function() {
Parse.$ = jQuery;
// Replace this line with the one on your Quickstart Guide Page
Parse.initialize("parseID",
"javascriptID");
init();
});
function init()
{
currentUser = Parse.User.current();
loginStatus();
} // init
function loginStatus()
{
// var currentUser = Parse.User.current();
if (currentUser) {
// do stuff with the user
new blogs();
} else {
// show the signup or login page
new LoginView();
// }
LoginView = Parse.View.extend({
template: Handlebars.compile($('#login-tpl').html()),
events: {
'submit .form-signin': 'login'
},
login: function(e) {
// Prevent Default Submit Event
e.preventDefault();
// Get data from the form and put them into variables
var data = $(e.target).serializeArray(),
username = data[0].value,
password = data[1].value;
// Call Parse Login function with those variables
Parse.User.logIn(username, password, {
// If the username and password matches
success: function() {
blogs();
},
// If there is an error
error: function(user, error) {
console.log(error);
}
});
},
render: function(){
this.$el.html(this.template());
}
});
function blogs() {
// var user = Parse.User.current();
var Blog = Parse.Object.extend("Post");
var Blogs = Parse.Collection.extend({
model: Blog,
query: (new Parse.Query(Blog)).equalTo("author", Parse.User.current())
});
var BlogsView = Parse.View.extend({
template: Handlebars.compile($('#blogs-tpl').html()),
render: function(){
var collection = { blog: this.collection.toJSON() };
this.$el.html(this.template(collection));
}
});
var blogs = new Blogs();
blogs.fetch({
success: function(blogs) {
var blogsView = new BlogsView({ collection: blogs });
blogsView.render();
$('.main-container').html(blogsView.el);
}
})
};
// Render login view on page
// var loginView = new LoginView();
// loginView.render();
// $('.main-container').html(loginView.el);
Upvotes: 1
Views: 583
Reputation: 324
Try making the currentUser variable a global variable. The currentUser variable's value is currently stuck within the scope of the init() function. Once you move to the loginStatus() function, the currentUser variable is reset.
Try instantiating the variable before your given code something like this.
//instantiate on the global scope
var currentUser;
$(function() {
Parse.$ = jQuery;
// Replace this line with the one on your Quickstart Guide Page
Parse.initialize("parseID", "javascriptID");
loginStatus();
});
function loginStatus() {
currentUser = Parse.User.current();
if(currentUser){
newBlogs();
}else{
new LoginView();
}
}
....
Upvotes: 2