-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
173662a
commit 57ba297
Showing
12 changed files
with
1,207 additions
and
36 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { firebaseApp } from "../app"; | ||
import { DecodedIdToken, getAuth } from "firebase-admin/auth"; | ||
|
||
export async function getFirebaseUserFromReqAccessToken( | ||
authHeader: string | ||
): Promise<DecodedIdToken | undefined> { | ||
if (!authHeader) { | ||
return undefined; | ||
} | ||
const auth = getAuth(firebaseApp); | ||
const token = authHeader.split(" ")[1]; | ||
if (!token) { | ||
return undefined; | ||
} | ||
try { | ||
const decodedToken = await auth.verifyIdToken(token); | ||
return decodedToken; | ||
} catch (e) { | ||
console.error(e); | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved. | ||
Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected] | ||
The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license. | ||
*/ | ||
import { GraphQLObjectType } from "graphql"; | ||
import { DecodedIdToken } from "firebase-admin/auth"; | ||
import UserModel, { User } from '../../models/User'; | ||
import UserType from '../types/user'; | ||
|
||
export const loginFirebase = { | ||
type: UserType, | ||
resolve: async ( | ||
_root: GraphQLObjectType, | ||
args: {}, | ||
context: { firebaseUser: DecodedIdToken } | ||
): Promise<User> => { | ||
console.log(context.firebaseUser); | ||
if (!context.firebaseUser) { | ||
throw new Error("unauthenticated"); | ||
} | ||
const user = await UserModel.findOneAndUpdate( | ||
{ firebaseId: context.firebaseUser.uid }, | ||
{ | ||
email: context.firebaseUser.email || "", | ||
lastLoginAt: new Date(), | ||
}, | ||
{ upsert: true, new: true } | ||
); | ||
return user; | ||
}, | ||
}; | ||
|
||
export default loginFirebase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved. | ||
Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected] | ||
The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license. | ||
*/ | ||
|
||
import createApp, { appStart, appStop } from "app"; | ||
import { expect } from "chai"; | ||
import { Express } from "express"; | ||
import mongoUnit from "mongo-unit"; | ||
import request from "supertest"; | ||
import { getFirebaseToken, getToken } from "../../helpers"; | ||
|
||
describe.only("login-firebase", () => { | ||
let app: Express; | ||
beforeEach(async () => { | ||
await mongoUnit.load(require("test/fixtures/mongodb/data-default.js")); | ||
app = await createApp(); | ||
await appStart(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await appStop(); | ||
await mongoUnit.drop(); | ||
}); | ||
|
||
it(`can log in`, async () => { | ||
const token = getFirebaseToken({ uid: "5ffdf1231ee2c62320b49e99" }); | ||
|
||
const fetchResult = await request(app) | ||
.post("/graphql") | ||
.set("Authorization", `bearer ${token}`) | ||
.send({ | ||
query: `mutation FirebaseLogin { | ||
firebaseLogin { | ||
firebaseId | ||
name | ||
userRole | ||
friends | ||
lastLoginAt | ||
} | ||
}`, | ||
}); | ||
expect(fetchResult.status).to.equal(200); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters