Dart登录/注销示例 [英] Dart login/logout example

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

问题描述

我试图找到一个使用Dart进行用户身份验证的简单示例。到目前为止,我找到的最接近的是



Facebook登录对话框会将用户引向我们在config( / login / facebook )中指定的URL,那么我们的应用程序需要响应它。我让你处理路由你想要什么,但本质上服务器当它收到 / login / facebook 请求,它首先编码一些属性:

  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']);

您需要 import'dart:uri'as uri first。



这之后,有一些API向Facebook请求:

  http.read('https://graph.facebook.com/oauth/access_token?client_id=$appId&redirect_uri=$url&client_secret=$appSecret&code = $ code')。then((contents){
//contents看起来像:access_token = USER_ACCESS_TOKEN& expires = NUM​​BER_OF_SECONDS_UNTIL_TOKEN_EXPIRES
var parameters = QueryString.parse('?$ contents');
var accessToken = parameters ['access_token'];

//尝试收集用户信息
http.read('https://graph.facebook.com/me? access_token = $ accessToken')。then((contents){
var user = JSON.parse(contents);

print(user); //以此用户身份登录
});
});

我使用 HTTP 包和 QueryString package。



API请求后,您就可以获得用户信息。现在你可以做一些事情,如在会话中存储已验证的用户。您可以使用 HttpRequest.session 。要注销,只需从会话中删除数据。



这是大致上需要做的,以使Facebook身份验证工作的过程。您可以期待许多其他网站的类似工作流。您可能还需要/使用 OAuth2 package。



总结:




  • 创建Facebook开发者个人资料

  • 请记下一些配置中的应用程式ID等。

  • 建立正确的登入连结。

  • 将服务器端代码写到Facebook登录引发用户的位置。

  • 在服务器中编写一些API调用以检索经过身份验证的用户。



玩得开心!


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.

解决方案

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.).

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

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)

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'
    }
  },
};

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';
}

Now on your HTML you specify the link:

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

At this point, perhaps read the Facebook developer guides: https://developers.facebook.com/docs/reference/dialogs/oauth/

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']);

You need to import 'dart:uri' as uri first.

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.
  });
});

I'm using the HTTP package here and the QueryString package.

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.

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.

So to summarize:

  • Create Facebook developer profile and an app with the proper URLs.
  • Write down the app id's and such in some config.
  • Create the proper login link.
  • Write server side code to where the Facebook login throws the users.
  • Write some API calls in the server to retrieve the authenticated user.

Have fun!

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

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