Skip to content

Commit

Permalink
fix: auth with emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
jkklapp committed May 27, 2022
1 parent c53dd1c commit 545d473
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 32 deletions.
5 changes: 2 additions & 3 deletions backend/src/firebase/firebase-auth.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class FirebaseAuthStrategy extends PassportStrategy(
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
});
const firebase_params = {
const firebaseParams = {
type: process.env.FB_PARAMS_TYPE,
projectId: process.env.FB_PARAMS_PROJECT_ID,
privateKeyId: process.env.FB_PARAMS_PRIVATE_KEY_ID,
Expand All @@ -27,7 +27,7 @@ export class FirebaseAuthStrategy extends PassportStrategy(
clientC509CertUrl: process.env.FB_PARAMS_CLIENT_C509_CERT_URL,
};
this.defaultApp = firebase.initializeApp({
credential: firebase.credential.cert(firebase_params),
credential: firebase.credential.cert(firebaseParams),
});
}
async validate(token: string) {
Expand All @@ -38,7 +38,6 @@ export class FirebaseAuthStrategy extends PassportStrategy(
console.log(err);
throw new UnauthorizedException(err.message);
});

if (!firebaseUser) {
throw new UnauthorizedException();
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"serve": "PORT=8888 vue-cli-service serve",
"build:local": "vue-cli-service build --mode local --watch",
"build:local": "NODE_ENV=local vue-cli-service build --mode local --watch",
"build:preview": "vue-cli-service build --mode development",
"build": "vue-cli-service build --mode production",
"lint": "vue-cli-service lint --max-warnings=0",
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/auth/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getAuth as getFirebaseAuth, connectAuthEmulator } from 'firebase/auth';

export const getAuth = () => {
const auth = getFirebaseAuth();
if (process.env.NODE_ENV == 'local' && auth.emulatorConfig === null) {
connectAuthEmulator(auth, 'http://localhost:9099');
}
return auth;
};
12 changes: 3 additions & 9 deletions frontend/src/components/Dashboard.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { mount } from '@vue/test-utils';
import Dashboard from './Dashboard.vue';
import firebase from 'firebase/compat/app';
import { getAuth } from '../auth';
import { apiRequest } from '../store/actions/api';

jest.mock('firebase/compat/app', () => {
return {
auth: jest.fn(),
};
});
jest.mock('../auth');
jest.mock('../store/actions/api');

const POSTS_RESPONSE_FIXTURE = [
Expand All @@ -19,7 +15,7 @@ const POSTS_RESPONSE_FIXTURE = [

describe('Dashboard', () => {
beforeEach(() => {
firebase.auth.mockReturnValue({
getAuth.mockReturnValue({
onAuthStateChanged: jest.fn(() => ({ email: 'test' })),
});
apiRequest.mockResolvedValueOnce({ data: POSTS_RESPONSE_FIXTURE });
Expand Down Expand Up @@ -128,8 +124,6 @@ describe('Dashboard', () => {
message: 'Hello World',
});

console.log(wrapper.emitted('click')[0][0]);

done();
});
});
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<script>
import { mapGetters } from 'vuex';
import store from '../store';
import firebase from 'firebase/compat/app';
import { getAuth } from '../auth';
export default {
data() {
Expand All @@ -35,7 +35,7 @@ export default {
}),
},
mounted() {
firebase.auth().onAuthStateChanged((user) => {
getAuth().onAuthStateChanged((user) => {
if (user) {
store.dispatch('setUser', user);
store.dispatch('fetchPosts');
Expand Down
8 changes: 3 additions & 5 deletions frontend/src/components/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
</template>
<script>
import { mapGetters } from 'vuex';
import firebase from 'firebase/compat/app';
import { getAuth } from '../auth';
export default {
computed: {
appName: () => process.env.VUE_APP_NAME,
Expand All @@ -56,12 +55,11 @@ export default {
},
methods: {
signOut() {
firebase
.auth()
getAuth()
.signOut()
.then(() => {
this.$router.replace({
name: '',
name: 'Login',
});
})
.catch((err) => {
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/components/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
</template>

<script>
import firebase from 'firebase/compat/app';
import { getAuth } from '../auth';
export default {
data() {
Expand All @@ -97,8 +97,7 @@ export default {
},
methods: {
submit() {
firebase
.auth()
getAuth()
.createUserWithEmailAndPassword(this.form.email, this.form.password)
.then((data) => {
data.user
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/store/actions/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import firebase from 'firebase/compat/app';
import axios from 'axios';
import { getAuth } from '../../auth';

export const apiRequest = async (method, url, data) => {
const idToken = await firebase.auth().currentUser.getIdToken(true);
const idToken = await getAuth().currentUser.getIdToken(true);
return await axios({
method,
url: process.env.VUE_APP_API_BASE + url,
Expand Down
10 changes: 3 additions & 7 deletions frontend/src/store/actions/api.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { apiRequest } from './api';
import firebase from 'firebase/compat/app';
import { getAuth } from '../../auth';
import axios from 'axios';

jest.mock('firebase/compat/app', () => {
return {
auth: jest.fn(),
};
});
jest.mock('../../auth');
jest.mock('axios');

describe('apiRequest', () => {
Expand All @@ -16,7 +12,7 @@ describe('apiRequest', () => {
process.env = {
VUE_APP_API_BASE: 'https://my-api.com',
};
firebase.auth.mockReturnValue({
getAuth.mockReturnValue({
currentUser: {
email: 'example@gmail.com',
uid: 1,
Expand Down

0 comments on commit 545d473

Please sign in to comment.