飞镖登录/注销的例子 [英] Dart login/logout example

查看:162
本文介绍了飞镖登录/注销的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到达特用户身份验证的一个简单的例子。到目前为止,我已经找到了最接近的是<一个href=\"https://github.com/dart-lang/bleeding_edge/blob/master/dart/tests/standalone/io/http_auth_test.dart\">https://github.com/dart-lang/bleeding_edge/blob/master/dart/tests/standalone/io/http_auth_test.dart.任何人都可以直接或提供给我使用飞镖服务器端认证的工作示例。先谢谢了。

I'm trying to find a simple example of user authentication with Dart. So far the closest I've found is https://github.com/dart-lang/bleeding_edge/blob/master/dart/tests/standalone/io/http_auth_test.dart. Can anyone direct or provide me to a working example of server side authentication using Dart. Thanks in advance.

推荐答案

认证是一个巨大的话题,你没有指定多少你想要达到的目标,但让我引导你建立一个Facebook的身份验证您飞镖应用程序,因为我认为这是开始最简单的一种。为什么?因为我们不需要谈论一切可能的数据库,你可能会使用,或你如何设置你的数据库结构,模型等,你如何处理与安全(令牌生成,等等)。

Authentication is a vast topic and you didn't specify much what you want to achieve, but let me guide you to build a Facebook authentication for you Dart application, because I think it's the simplest one to begin with. Why? Because we don't need to talk about every possible database you might be using, or how you setup your database structure, models, etc. and how you deal with security (generating tokens, etc.).

通过这方面的知识,那么你可以实现其他服务认证(Google +和Twitter),并最终你自己,如果你愿意的话。

With this knowledge you can then implement other service authentications (Google+, Twitter) and eventually your own if you so wish.

因此​​,首先,注册在 Facebook的由pressing应用页面应用程序的创建新的应用程序。你应该得到这个页面:

So, first, register an application on Facebook apps page by pressing Create New App. You should get this page:

(请务必填写这两个应用程序域和网站网址)

(Be sure to fill both App domain and site URL)

然后某处指定一些配置文件(如 config.dart ),您将导入无处不在,你需要:

Then specify some configuration file somewhere (e.g. config.dart) that you will import everywhere you need:

var config = {
  'authentication': {
    'facebook': {
      'appId': '...',
      'appSecret': '...',
      'url': 'http://test.com/login/facebook'
    }
  },
};

然后你需要的地方创建一个链接。如果您正在使用的Web UI,你的飞镖脚本可以先导入配置文件以及创建一个登录网址:

Then you need to create a link somewhere. If you are using Web UI, your Dart script could first import the config and create a login URL:

import 'config.dart';

main() {
  var fbConfig = config['authentication']['facebook'];
  var appId = fbConfig['appId'];
  var url = fbConfig['url'];

  var loginLinkUrl = 'https://www.facebook.com/dialog/oauth/?client_id=$appId&redirect_uri=$url&state=TEST_TOKEN&scope=email';
}

现在在你的HTML您指定的链接:

Now on your HTML you specify the link:

<a href="{{ loginLinkUrl }}">Login with Facebook</a>

在这一点上,也许读Facebook开发指南:<一href=\"https://developers.facebook.com/docs/reference/dialogs/oauth/\">https://developers.facebook.com/docs/reference/dialogs/oauth/

Facebook的登录对话框将引发用户我们的配置( /登录/ Facebook的),那么我们的应用需求作出回应指定的URL。我让你处理路由怎么过你想要的,但本质上是服务器在接收到 /登录/ Facebook的请求时,首先连接codeS的一些属性:

Facebook login dialog will throw the user to the URL we specified in config (/login/facebook), then our application needs respond to it. I let you handle the routing how ever you want, but essentially the server when it receives a /login/facebook request, it first encodes some properties:

var code = uri.encodeUriComponent(request.queryParameters['code']);
var appId = uri.encodeUriComponent(config['authentication']['facebook']['appId']);
var appSecret = uri.encodeUriComponent(config['authentication']['facebook']['appSecret']);
var url = uri.encodeUriComponent(config['authentication']['facebook']['url']);

您需要进口'镖:URI'。作为URI 第一

在此之后,有点code的,做API请求给Facebook:

After this, a bit of code that does API requests to Facebook:

http.read('https://graph.facebook.com/oauth/access_token?client_id=$appId&redirect_uri=$url&client_secret=$appSecret&code=$code').then((contents) {
  // "contents" looks like: access_token=USER_ACCESS_TOKEN&expires=NUMBER_OF_SECONDS_UNTIL_TOKEN_EXPIRES
  var parameters = QueryString.parse('?$contents');
  var accessToken = parameters['access_token'];

  // Try to gather user info.
  http.read('https://graph.facebook.com/me?access_token=$accessToken').then((contents) {
    var user = JSON.parse(contents);

    print(user); // Logged in as this user.
  });
});

我使用的是 HTTP 包这里的的 QUERYSTRING 包。

在API请求后,您手边的用户信息。现在,你可以做这样的事情在会话中存储的身份验证的用户。您可以使用例如<一href=\"http://api.dartlang.org/docs/bleeding_edge/dart_io/Htt$p$pquest.html#session\"><$c$c>Htt$p$pquest.session以此目的。要注销,只需从会话删除数据。

After the API requests, you have the user information at hand. Now you can do things like store the authenticated user in a session. You can use e.g. HttpRequest.session for this purpose. To logout, just remove the data from the session.

这大约是你需要做的起床Facebook验证工作的过程。你可以期待许多其他网站类似的工作流程。您可能还需要/使用 的OAuth2 包。

This is roughly the procedure you need to do to get up Facebook authentication to work. You can expect similar workflows for many other websites. You might also need/use the OAuth2 package.

总结一下:


  • 创建Facebook的开发人员配置,并用正确的网址的应用程序。

  • 写下应用程序ID和这样的一些配置。

  • 创建正确的登录链接。

  • 写入服务器端code到Facebook登录抛出的用户。

  • 将在服务器中某些API调用来检索身份验证的用户。

玩得开心!

这篇关于飞镖登录/注销的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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