Feras,

My point is that you'll find your application easy to manage if you keep
your login code (authenticate) separate from your presentation code
(JQuery). See below for an (untested) example based on your original code:

It'll also help you if you explain what exactly is happening that you don't
expect. If you just say it "isn't working" then its much harder to for us
to help.

Good luck!


// Server JS
app.get('/', function(req, res, next) {
    passport.authenticate('local', function(err, user, info) {
        if (err) return next(err);
        if (!user) return res.send(401);
        // Not sure what the difference between authenticate and logIn is
in this context. Persistence maybe?
        req.logIn(user, function(err) {
            if (err) return next(err);
            return res.send(200);
        });
    })(req, res, next);
});

// Client JS
$.get("/")
    .failure(function(){
        $("#error")
            .text("YOUR MESSAGE")
            .show()
            .delay(800)
            .fadeOut(400);
    })
    .success(function(){
        document.location.href = "/account"
    })



On 24 April 2012 12:58, Feras Odeh <feras...@gmail.com> wrote:

> Hello Richard,
>
> All I'm trying to do is to send a JQuery error message to the User that
> tell him what is the error. How could send it or just to notify the user of
> the error without any redirection (using XHR).
>
> Thanks,
> Feras
>
>
> On Tuesday, April 24, 2012 2:31:10 PM UTC+3, Richard Marr wrote:
>>
>> Not sure what the main problem is as I'm not familiar with the tools and
>> don't have enough info to guess, but there are a couple of style points
>> that I'd make:
>>
>>    - You seem to have a load of jQuery code in your Express response.
>>    I'd recommend staying well away from that approach for most applications 
>> as
>>    it's harder to control in terms of separation of concerns, encapsulation,
>>    caching, etc.
>>    - Your function looks like it will return either an HTML fragment or
>>    an HTTP redirect, which to me is a confusing combination, do you mean to
>>    redirect the entire browser or just tell the XHR object to fetch a
>>    different fragment from "/account"?
>>
>> Also, if you're stuck it's helpful to throw a few console.log()
>> statements in there and make sure what's happening matches your
>> expectations.
>>
>>
>>
>> On 24 April 2012 08:54, Feras Odeh <feras...@gmail.com> wrote:
>>
>>> I want to show error messages for users in cases of wrong credentials.
>>> How could I do that in Express JS? I tried the following code but it
>>> doesn't work:
>>>
>>> app.get('/', function(req, res, next) {
>>>         passport.authenticate('local', function(err, user, info) {
>>>         if (err) { return next(err) }
>>>         if (!user) { return res.send($( "<div class='ui-loader 
>>> ui-overlay-shadow ui-body-e ui-corner-all'><h1>YOUR MESSAGE</h1></div>" )
>>>         .css({ "display": "block", "opacity": 0.96, "top": 
>>> $(window).scrollTop() + 100 })
>>>         .appendTo( $.mobile.pageContainer )
>>>         .delay( 800 )
>>>         .fadeOut( 400, function() {
>>>         $( this ).remove();
>>>         })
>>>         );  }
>>>         req.logIn(user, function(err) {
>>>         if (err) { return next(err); }
>>>         return res.redirect('/account');
>>>         });
>>>         })(req, res, next);
>>>     });
>>>
>>> What is the problem with this?
>>>
>>> Thanks,
>>>
>>> Feras
>>>
>>> --
>>> Job Board: http://jobs.nodejs.org/
>>> Posting guidelines: https://github.com/joyent/**node/wiki/Mailing-List-*
>>> *Posting-Guidelines<https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines>
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To post to this group, send email to nodejs@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> nodejs+unsubscribe@**googlegroups.com<nodejs%2bunsubscr...@googlegroups.com>
>>> For more options, visit this group at
>>> http://groups.google.com/**group/nodejs?hl=en?hl=en<http://groups.google.com/group/nodejs?hl=en?hl=en>
>>>
>>
>>
>>
>> --
>> Richard Marr
>>
>  --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>



-- 
Richard Marr

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to