Backbone.js的和用户身份验证 [英] Backbone.js and user authentication

查看:98
本文介绍了Backbone.js的和用户身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在琢磨了好一阵子我怎么会去使用骨干,因为我一直在阅读关于它的一些文章,他们中的很多都在谈论令牌和密钥验证用户。但我只是希望能够在用户登录和注册像你这样的用户通常会。

I have been wondering for quite a while how I would go about authenticating users using Backbone because I have been reading a few articles about it and a lot of them are talking about tokens and keys.. But I just want to be able to sign in a user and register a user like you would normally.

我在想,在Web应用程序启动时会有到路由请求/我,然后服务器给用户返回相应的信息,如果他在她记录/

I was thinking that on the web app start up there would be a request to the route '/me' and then the server gives the user back appropriate information if he/she is logged in.

一样,如果途径带回来 {的loggedIn:假} 的骨干路由器将用户发送到登录/注册只页面。但是,如果它与用户的配置文件信息回来那就意味着显然他有一个会议。

Like if the route came back with {loggedIn: false} the backbone router would send the user to the login/register pages only. But if it came back with a users profile information then it would obviously mean he had a session.

这是使用主干时,回到用户身份验证的方法好吗?

Is this an okay way of going back user authentication when using Backbone?

推荐答案

简短的回答:线材高达$就向401状态$ C $回应CS

Short answer: wire up $.ajax to respond to 401 (Unauthorized) status codes.

龙答:我们正在消耗从单页网站一个RESTful API。当服务器检测到未经授权的请求,它只是返回一个401客户端将重定向到/登录?#请求/资源。

Long answer: We're consuming a RESTful api from a single page website. when the server detects an unauthorized request, it just returns a 401. The client will redirect to /login?#requested/resource.

/登录会提示进行授权(重定向到我们的情况下,谷歌的誓言服务器),然后添加一个授权cookie,并重定向到最初请求#要求/资源

/login will prompt for authorization (redirect to google's oath server in our case) then add an authorization cookie and redirect to the originally requested #requested/resource

我们也送每$阿贾克斯要求AUTH的cookie。

we're also sending the auth cookie on every $.ajax request.

希望这是有帮助的。

define(
    [
        'jquery',
        'jquery.cookie'
    ],
    function ($) {
        var redirectToLogin = function () {
            var locationhref = "/login";
            if (location.hash && location.hash.length > 0) {
                locationhref += "?hash=" + location.hash.substring(1);
            }
            location.href = locationhref;
        };

        var $doc = $(document);
        $doc.ajaxSend(function (event, xhr) {
            var authToken = $.cookie('access_token');
            if (authToken) {
                xhr.setRequestHeader("Authorization", "Bearer " + authToken);
            }
        });

        $doc.ajaxError(function (event, xhr) {
            if (xhr.status == 401)
                redirectToLogin();
        });
    });

这篇关于Backbone.js的和用户身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆