mercredi 27 janvier 2016

AngularJS and SQlite - Store Facebook Token Data

I want store user data by Facebook token.

I use ionic with AngularJS and Cordova.

I try this :

App.JS

var myApp = angular.module('starter', ['ionic', 'starter.controllers','ngCordova','ui.router'])

var db = null;

bewelk.run(function($ionicPlatform, $rootScope, $state, $cordovaSQLite) {

  $ionicPlatform.ready(function() {

if (window.cordova) {
  db = $cordovaSQLite.openDB("myApp_user.db"); //device
} else {
  db = window.openDatabase("myApp_user.db", '1', 'myApp_user', 1024 * 1024 * 100); // browser
}
   $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS user_data (id integer primary key, user_id text,first_name text)");

 })
})

And in my Auth.JS

var fbLoginSuccess = function(response) {
    if (!response.authResponse) {
      fbLoginError("Cannot find the authResponse')")
      return
    }
    console.log('fbLoginSuccess')

    var authResponse = response.authResponse

    getFacebookProfileInfo(authResponse)
      .then(function(profileInfo) {

        Ionic.io();
        var ionicUser = Ionic.User.current()
        if (!ionicUser.id) {
          ionicUser.id = Ionic.User.anonymousId()
        }
        ionicUser.save()

        UserService.setUser({
          authResponse: authResponse,
          name: profileInfo.name,
          user_id: profileInfo.id,
          first_name: profileInfo.first_name,
          picture: profileInfo.picture.data.url,
          ionic_id: ionicUser.id
        })



        if (window.cordova) {
            db = $cordovaSQLite.openDB("myApp_user.db"); //device
          } else {
            db = window.openDatabase("myApp_user.db", '1', 'myApp_user', 1024 * 1024 * 100); // browser
         }

        $cordovaSQLite.execute(db, "INSERT INTO user_data(user_id, first_name) VALUES (profileInfo.id,profileInfo.first_name)");


      })
    }

My UserService in localStorage works like a charm (But not persistent if memory is low), but SQLite don't write data in myApp_user.db (However, the database is created in my device).

How can I write correctly this data in my SQLite database ?

Aucun commentaire:

Enregistrer un commentaire