Skip to content

Instantly share code, notes, and snippets.

@gambare
Last active December 26, 2016 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gambare/49354047b390999756b88130b8262544 to your computer and use it in GitHub Desktop.
Save gambare/49354047b390999756b88130b8262544 to your computer and use it in GitHub Desktop.
webviewでGoogleのOAuth認証を行う ref: http://qiita.com/gambare/items/7af00ad4c953aed9b11e
import {AngularFireModule} from 'angularfire2';
~省略~
@NgModule({
declarations: [
~省略~
],
imports: [
AngularFireModule.initializeApp(
firebaseConfig,
firebaseAuthConfig
),
],
~省略~
// 以下の値は、apiKeyなどの取得で取得した値。
export const firebaseConfig = {
apiKey: 'APIキー',
authDomain: 'ドメイン',
databaseURL: 'URL',
storageBucket: 'ストレージバケット'
};
// 以下の値は、認証設定。Google認証をリダイレクトで行う。
export const firebaseAuthConfig = {
provider: AuthProviders.Google,
method: AuthMethods.Redirect
};
<?xml version='1.0' encoding='utf-8'?>
<widget id="ここです。" ...>
...
</widget>
ionic start --v2 <プロジェクト名>
# angular cliの場合 ng new <プロジェクト名>
cd <プロジェクト名>
npm install angularfire2 firebase --save
keytool -exportcert -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore
SHA1: 〜〜〜〜ハッシュ値〜〜〜〜
# npmから取得する場合
cordova plugin add cordova-plugin-googleplus --save --variable REVERSED_CLIENT_ID=ここにiOSの手順で取得したIDを入れる
# githubから最新を取得する場合
cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-googleplus --save --variable REVERSED_CLIENT_ID=ここにiOSの手順で取得したIDを入れる
cordova prepare
login(){
window['plugins'].googleplus.login(
{
// 'scopes': '',
'webClientId': 'ここにAndroidの手順で取得したgoogle-services.jsonのclient-idを入れる',
'offline': false,
},
function (authData) {
alert(JSON.stringify(authData)); // do something useful instead of alerting
},
function (msg) {
alert('error: ' + msg);
}
);
}
ionic run android --consolelogs --livereload --device
ngOnInit(){
window['plugins'].googleplus.login(
{
// 'scopes': '',
'webClientId': 'ここにAndroidの手順で取得したgoogle-services.jsonのclient-idを入れる',
'offline': false,
},
function (authData) {
let credential = (<any>firebase.auth.GoogleAuthProvider).credential(authData.idToken);
firebase.auth().signInWithCredential(credential);
},
function (msg){
console.log('acquireAuth error: ' + msg);
}
);
}
cordova run android --device
import { AngularFire,
FirebaseAuthState} from 'angularfire2';
@Component({
templateUrl: 'テンプレート名.html'
})
export class Test{
constructor(private af :AngularFire){}
// ログインメソッド。templateのボタンクリックで呼び出される。
login() :firebase.Promise<FirebaseAuthState>{
return this.af.auth.login();
}
}
ionic serve
# Angularの場合 ng serve
ionic platform add android
ionic run android --consolelogs --livereload --device
# avdで起動する場合、 --deviceは不要
"oauth_client": [
{
"client_id": "ここです",
"client_type": 3
}
<key>REVERSED_CLIENT_ID</key>
<string>ここです</string>
<!-- ページにボタンを追加します。前後のコードは省きます。 -->
<button ion-button (click)="login()">Login</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment