@@ -4,6 +4,18 @@ import { NextPage } from 'next';
4
4
import React from 'react' ;
5
5
import TwitterIcon from '@material-ui/icons/Twitter' ;
6
6
7
+ import firebase from 'firebase/app' ;
8
+ import 'firebase/auth' ;
9
+
10
+ import { firebaseConfig } from '../utils/firebaseConfig' ;
11
+
12
+ /**
13
+ * ---API実装時に見るメモ---
14
+ * idTokenをheadsに入れてサーバーに送信
15
+ * userNameはtwitterのユーザー名(bodyに入れてサーバーに送信)
16
+ * ⇒ 以上の2つの情報をサーバーに送信する
17
+ */
18
+
7
19
const useStyles = makeStyles ( ( theme : Theme ) =>
8
20
createStyles ( {
9
21
displayBox : {
@@ -49,6 +61,76 @@ const useStyles = makeStyles((theme: Theme) =>
49
61
const Login : NextPage = ( ) => {
50
62
const styles = useStyles ( ) ;
51
63
64
+ function submit ( ) : void {
65
+ // 初期化は一度だけ(!!重要!!)
66
+ if ( ! firebase . apps . length ) {
67
+ firebase . initializeApp ( firebaseConfig ) ;
68
+ }
69
+
70
+ // 認証処理
71
+ // signInWithPopupメソッドを叩くと、認証用のポップアップ画面が表示される。
72
+ // それにTwitterのIDとパスワードを入力すると、コールバックをfirebase側が処理し、
73
+ // 認証成功時はPromise型で認証情報を返す
74
+ const provider = new firebase . auth . TwitterAuthProvider ( ) ;
75
+ firebase
76
+ . auth ( )
77
+ . signInWithPopup ( provider )
78
+ . then ( ( result ) => {
79
+ /** @type {firebase.auth.OAuthCredential } */
80
+ const credential = result . credential as firebase . auth . OAuthCredential ;
81
+
82
+ // This gives you a the Twitter OAuth 1.0 Access Token and Secret.
83
+ // You can use these server side with your app's credentials to access the Twitter API.
84
+ const token = credential . accessToken ;
85
+ console . log ( 'token:' + token ) ;
86
+ const secret = credential . secret ;
87
+ console . log ( 'secret:' + secret ) ;
88
+
89
+ // The signed-in user info.
90
+ const user = result . user ;
91
+ console . log ( user ) ;
92
+ // userNameはtwitterのユーザー名(bodyに入れてサーバーに送信)
93
+ const userName = user . displayName ;
94
+ console . log ( userName ) ;
95
+ // ローカルストレージにuserNameを保存
96
+ localStorage . setItem ( 'userName' , userName ) ;
97
+
98
+ // userIconImageはtwitterのアイコン画像(サーバーに送信しない)
99
+ const userIconImage = user . photoURL ;
100
+ console . log ( userIconImage ) ;
101
+ // ローカルストレージにuserIconImageの画像パスを保存
102
+ localStorage . setItem ( 'userIconImage' , userIconImage ) ;
103
+
104
+ firebase
105
+ . auth ( )
106
+ . currentUser . getIdToken ( /* forceRefresh */ true )
107
+ . then ( function ( idToken ) {
108
+ // idTokenをheadsに入れてサーバーに送信
109
+ // idTokenはローカルストレージに保存する
110
+ console . log ( idToken ) ;
111
+ //ローカルストレージにTokenを保存
112
+ localStorage . setItem ( 'Token' , idToken ) ;
113
+ } )
114
+ . catch ( function ( error ) {
115
+ window . alert ( 'error can not get current user:' + error ) ;
116
+ } ) ;
117
+ // ...
118
+ } )
119
+ . catch ( ( error ) => {
120
+ // Handle Errors here.
121
+ const errorCode = error . code ;
122
+ console . log ( errorCode ) ;
123
+ const errorMessage = error . message ;
124
+ console . log ( errorMessage ) ;
125
+ // The email of the user's account used.
126
+ const email = error . email ;
127
+ console . log ( email ) ;
128
+ // The firebase.auth.AuthCredential type that was used.
129
+ const credential = error . credential ;
130
+ console . log ( credential ) ;
131
+ // ...
132
+ } ) ;
133
+ }
52
134
return (
53
135
< Box className = { styles . displayBox } borderRadius = { 16 } >
54
136
< Box >
@@ -60,7 +142,7 @@ const Login: NextPage = () => {
60
142
</ Typography >
61
143
</ Box >
62
144
< Box className = { styles . buttonWrapper } >
63
- < Button className = { styles . loginButton } >
145
+ < Button className = { styles . loginButton } onClick = { submit } >
64
146
< TwitterIcon />
65
147
Login with Twitter
66
148
</ Button >
0 commit comments