Skip to content

Commit 52ad9e3

Browse files
authored
Merge pull request #42 from openhacku-saboten/u-tan/login
firebaseの認証( `login` )
2 parents 785b57e + 7c0ab61 commit 52ad9e3

File tree

4 files changed

+660
-6
lines changed

4 files changed

+660
-6
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@material-ui/lab": "^4.0.0-alpha.57",
1414
"@monaco-editor/react": "^4.1.0",
1515
"axios": "^0.21.1",
16+
"firebase": "^8.3.1",
1617
"marked": "^2.0.1",
1718
"next": "10.0.8",
1819
"react": "17.0.1",

pages/login.tsx

+83-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ import { NextPage } from 'next';
44
import React from 'react';
55
import TwitterIcon from '@material-ui/icons/Twitter';
66

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+
719
const useStyles = makeStyles((theme: Theme) =>
820
createStyles({
921
displayBox: {
@@ -49,6 +61,76 @@ const useStyles = makeStyles((theme: Theme) =>
4961
const Login: NextPage = () => {
5062
const styles = useStyles();
5163

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+
}
52134
return (
53135
<Box className={styles.displayBox} borderRadius={16}>
54136
<Box>
@@ -60,7 +142,7 @@ const Login: NextPage = () => {
60142
</Typography>
61143
</Box>
62144
<Box className={styles.buttonWrapper}>
63-
<Button className={styles.loginButton}>
145+
<Button className={styles.loginButton} onClick={submit}>
64146
<TwitterIcon />
65147
Login with Twitter
66148
</Button>

utils/firebaseConfig.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const firebaseConfig = {
2+
apiKey: 'AIzaSyANZ7TpygQkvJuRu7PyIuT8jIdELSdijPg',
3+
authDomain: 'omniscode-31b23.firebaseapp.com',
4+
projectId: 'omniscode-31b23',
5+
storageBucket: 'omniscode-31b23.appspot.com',
6+
messagingSenderId: '1031677260576',
7+
appId: '1:1031677260576:web:7f63ca23496d71deeb1d45',
8+
measurementId: 'G-EX6EXHMLGV',
9+
};

0 commit comments

Comments
 (0)