Dart Entry / Exit Example

I am trying to find a simple example of user authentication using Dart. So far, the closest I have 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 with a working example of server-side authentication using Dart. Thanks in advance.

+8
source share
3 answers

Authentication is an extensive topic, and you have not indicated much that you want to achieve, but let me direct you to create Facebook authentication for you, the Dart application, because I think this is the easiest way to get started. What for? Because we don’t need to talk about every possible database that you could use, or about how you set up the database structure, models, etc., and how you deal with security (generate tokens, etc. )

With this knowledge, you can implement other service authentication (Google+, Twitter) and, ultimately, your own if you want.

So, first register the application on the Facebook apps page by clicking Create a new application. You should get this page:

enter image description here

(Be sure to fill in both domain and site domains)

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

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

Then you need to create the link somewhere. If you use the web interface, your Dart script can first import the configuration 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 are giving the link:

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

For now, perhaps read the Facebook developer guides: https://developers.facebook.com/docs/reference/dialogs/oauth/

In the Facebook login dialog box, the user specified in config ( /login/facebook ) will be indicated, then our application should respond to it. I let you handle the routing as you want, but essentially the server, when it receives the /login/facebook request, 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']); 

First you need import 'dart:uri' as uri .

After that, some code that requests the API for 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 use HTTP here and QueryString .

After API requests, you get user information. Now you can do things like keep an authenticated user in a session. You can use for example. HttpRequest.session for this purpose. To log out, simply delete the data from the session.

This is roughly what you need to do to activate Facebook authentication to work. You can expect similar workflows for many other websites. You may also need / use the OAuth2 package.

So, we summarize:

  • Create a Facebook developer profile and application with the appropriate URLs.
  • Record the application identifier, etc. in some configuration.
  • Create the correct login link.
  • Enter the code on the server side where Facebook users find users.
  • Write some API calls on the server to get an authenticated user.

Good luck

+14
source

I wrote part 3 of a Dart login / authorization tutorial using the Rikulo security feature.

Security class login is direct. In any case, the standard example needs some changes to suit your specific requirements, especially routing / redirection after actions.

In addition, the tutorial includes a dummy user input, which is obviously not an option for production. So you need @override a standard feature to log in from Rikulo by creating your own Authenticator

 class _Authenticator extends Authenticator { @override Future login(HttpConnect connect, String username, String password) {...}; } 

Then go ahead and talk to your server and return either the user object on successful login, or an error on error!

Hope this helps you!

+2
source

I am very pleased to read your context. I need examples with an angled dart. Could you post your project to git hub so that I can read your source code.

0
source

All Articles