colinskow / superlogin

Powerful authentication for APIs and single page apps using the CouchDB ecosystem which supports a variety of providers.
MIT License
371 stars 117 forks source link

How to authenticate on pouchdb client with Ionic 2? #89

Closed dhndeveloper closed 8 years ago

dhndeveloper commented 8 years ago

Hello,

I'm really new to working with authentication and databases. I was able to follow the readme and the demo fairly well.

I think the answer to my question is somewhere between:

new SuperLogin(config, passport, userDB, couchAuthDB) and the directions below it and the "Client Access Token for Cordova"

I'm working on an offline first Ionic 2 application. I'll be using PouchDB and syncing with Cloudant. I have the "getting started" script on a server.js file on Node. My "Sign Up" form in Ionic does a http.post request to /auth/register. Its so awesome to see the user get added to the sl-users database and see the personal DB get setup.

I need to connect the dots on authenticating a user in pouchdb then syncing the databases.

Any help would be much appreciated. At this setup, I need help with the login() function which I know will need to connect to a pouchdb in a service.

` var express = require('express'); var http = require('http'); var bodyParser = require('body-parser'); var logger = require('morgan'); var cors = require('cors'); var SuperLogin = require('superlogin');

var app = express();

app.set('port', 3000);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());

var config = {
  dbServer: {
    protocol: 'http://', /* https:// */
    host: 'localhost:5984', /*'username.cloudant.com',*/
    user: 'user',
    password: 'password', /*'pass'*/
    // cloudant: true,
    userDB: 'sl-users',
    couchAuthDB: '_users'
  },
  mailer: {
    fromEmail: 'gmail.user@gmail.com',
    options: {
      service: 'Gmail',
        auth: {
          user: 'gmail.user@gmail.com',
          pass: 'userpass'
        }
    }
  },
  userDBs: {
    defaultDBs: {
      private: ['supertest']
    }
  },
  providers: { local: true }
}

// Initialize SuperLogin
var superlogin = new SuperLogin(config);

// Mount SuperLogin's routes to our app
app.use('/auth', superlogin.router);

http.createServer(app).listen(app.get('port'));

`

Login.ts

`import {Component} from '@angular/core'; import {NavController, MenuController} from 'ionic-angular'; import {DashboardPage} from '../dashboard/dashboard'; import {Http, Headers} from '@angular/http';

@Component({
  templateUrl: 'build/pages/login/login.html',
})
export class LoginPage {

  public newCreds: Object;

  constructor(public http: Http, public nav: NavController, public menu: MenuController) {
    this.menu.enable(false);

    this.newCreds = {
      name: '',
      username: '',
      password: '',
      confirmPassword: ''
    }

  }

  login(){
    this.menu.enable(true);
    this.nav.setRoot(DashboardPage);
  }

  SignUp(newCreds){

    //connects to express server and uses superlogin to create a new user and user database!

    let body = JSON.stringify(newCreds);
    let headers = new Headers();

    headers.append('Content-Type', 'application/json');

    this.http.post('http://localhost:3000/auth/register', body, {headers: headers}).subscribe(data => {
      console.log("success");
    }, error => {
      console.log(JSON.stringify(error.json()));
    });
  }
}`
colinskow commented 8 years ago

I haven't yet worked with Angular2 yet, but you can see exactly how to set things up server-side with SuperLogin-Demo. As far as client-side code you may be able to use SuperLogin-Client.

I would definitely welcome the creation of an Angular2 version of NG-Superlogin.

numerized commented 8 years ago

This might help a lot.

http://www.joshmorony.com/part-2-creating-a-multiple-user-app-with-ionic-2-pouchdb-couchdb/

dhndeveloper commented 8 years ago

Thanks! I actually reached out to him prior to that and he was able to help me... I'll close this.

tonydiep commented 7 years ago

Does anyone have a working version of ionic2 -> superlogin using with social authentication? The joshmorony blog above is excellent but it does not cover logging in using social.