awesome-auth-client
Authenticating, awesomely
Last updated 6 years ago by khughes .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install awesome-auth-client 
SYNC missed versions from official npm registry.

Overview

The client library for awesome-auth used to register and authenticate users and securely connect to endpoints via websocket. It consists of two classes, AwesomeAuth and AuthSocket.

  • AwesomeAuth is used to check authentication status, get info about the current user and register a new user to your application.
  • AuthSocket is used to securely make websocket calls to your http or websocket endpoints.

Get the javascript client

The javascript client is available on npm. Browserify is recommended.

npm install awesome-auth-client

Other options include manually pulling down the most recent release from the repo or referencing the hosted latest version. Include the AwesomeAuth javascript client in your web page to make the AwesomeAuth and AuthSocket modules available.

<script src="awesome-auth.js"></script>

Authentication status

To check user authentication status and identify the current user, create a new instance of the AwesomeAuth object and pass it an options JSON object.

new AwesomeAuth(opts)

  • appName - the name of your mapping file
  • onAuthStatusChange - function that is called with the authentication status when the user's authentication status changes.
    • result.user - If the user has confirmed, this will populated with the user defined in the getUserInfo function
    • result.status - Contains a string representing a failure to authenticate. May be 'user-not-found' or 'unconfirmed'.
    • result.error - If there's a system error during processing
var awesomeAuth = new AwesomeAuth({appName: 'pocket', onAuthStatusChange: function(result){
    if (result.user){
        console.log("Logged in user:", result.user)
        window.currentUser = result.user
    }
    else if (result.status) {
        console.log("Need to register or confirm registration", result.status)
    }
    else if (result.error) {
        console.log("System error", result.error)
    }
}});

Registration

awesomeAuth.register('someEmail@domain.com', function(err, token){
    if (err) console.log("err", err);
    if (token) console.log("token", token);
});
awesomeAuth.register({email: 'someEmail@domain.com', customerName: "Bain", person_id: 1594}, function(err, token){
    if (err) console.log("err", err);
    if (token) console.log("token", token);
}));

This registers a user to the system and sends them a confirmation email. The optional callback function can report that a token was generated and sending an email has been attempted.

AwesomeAuth will continuously poll until they have clicked the link in the email. Once they have confirmed, the onAuthStatusChange callback will return the full user defined in the getUserInfo function.

The data passed to register gets sent to the getUserInfo function, so include anything you need to identify this user.

Resend confirmation

If a user misses the initial confirmation email, they can self-serve and trigger the sending of another.

awesomeAuth.resendConfirmation(function(err){
    if (err) {
        console.log("Error while sending confirmation email", err);
    } else {
        console.log("Confirmation email sent!");
    }
});

Log out

Logging a user out destroys their token. They have to re-register to get back into the system.

awesomeAuth.logOut();

This will trigger the onAuthStatusChange callback with the status "user-log-out". ###Using the authorized socket To initialize, create an instance of AuthSocket and pass in the options object to the constructor.

var opts = {
    appName: 'pocket',
    urls: ["http://query.glgroup.com/epiquery/"]
};

var mySocket = new AuthSocket(opts);

opts

After it has been created, it behaves exactly like a normal websocket. All of the normal Websocket events are exposed on the AuthSocket:

mySocket.onerror = function(error){
    console.log(error)
};

mySocket.onmessage = function(message) {
    console.log("Message from destination server", message)
};

mySocket.onclose = function(event){
    console.log("Connection closed")
};

mySocket.onopen = function(){
    console.log("Connection opened", socket.currentUser)
};

###Sending a message to an HTTP endpoint

HTTP messages will use the send method on the socket and pass an options object.

options verb - HTTP method path - the path to your destination relative to the url(s) provided when creating the socket callback - function that handles results from your call

var opts = {
    verb: 'GET',
    path: 'person/getPersonByLoginName.mustache?Login=glgroup\\khughes'
    callback: function(results) {
        console.log("Here are my results!", results)
    }
}
mySocket.send(opts);

Working offline

There's a development mode that you can use to work completely disconnected from AwesomeAuth. You can use it to fake out a user and to communicate with WebSocket and HTTP servers.

Please note, it has limited functionality. You can't register or log out and it's not performing any authentication. It also doesn't connect to basic auth protected services.

Here's an example of faking out a user.

var myUser = {
  name: "Nick Swarr",
  email: "nswarr@glgroup.com",
  id: 123456
};
  
var awesomeAuth = new AwesomeAuth({user: myUser, authenticationCallback: function(result){
  console.log("Just got my user back!", result.user);
}});

Here's how you set up a local socket.

var authSocket = new AuthSocket({localDev: true, urls: ["ws://localhost:5555/"]});
authSocket.send("This talks directly to my socket server")
``




Current Tags

  • 0.0.44                                ...           latest (6 years ago)

39 Versions

  • 0.0.44                                ...           6 years ago
  • 0.0.43                                ...           6 years ago
  • 0.0.42                                ...           6 years ago
  • 0.0.41                                ...           6 years ago
  • 0.0.40                                ...           6 years ago
  • 0.0.39                                ...           6 years ago
  • 0.0.38                                ...           6 years ago
  • 0.0.37                                ...           6 years ago
  • 0.0.36                                ...           6 years ago
  • 0.0.35                                ...           6 years ago
  • 0.0.34                                ...           6 years ago
  • 0.0.33                                ...           6 years ago
  • 0.0.31                                ...           6 years ago
  • 0.0.30                                ...           6 years ago
  • 0.0.28                                ...           6 years ago
  • 0.0.27                                ...           6 years ago
  • 0.0.25                                ...           6 years ago
  • 0.0.24                                ...           6 years ago
  • 0.0.23                                ...           6 years ago
  • 0.0.22                                ...           6 years ago
  • 0.0.21                                ...           6 years ago
  • 0.0.20                                ...           6 years ago
  • 0.0.19                                ...           6 years ago
  • 0.0.18                                ...           6 years ago
  • 0.0.17                                ...           6 years ago
  • 0.0.16                                ...           6 years ago
  • 0.0.15                                ...           6 years ago
  • 0.0.13                                ...           6 years ago
  • 0.0.12                                ...           6 years ago
  • 0.0.11                                ...           6 years ago
  • 0.0.10                                ...           6 years ago
  • 0.0.9                                ...           6 years ago
  • 0.0.8                                ...           6 years ago
  • 0.0.7                                ...           6 years ago
  • 0.0.6                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (4)
Dev Dependencies (14)

Copyright 2014 - 2016 © taobao.org |