chore: Use currentUser from authentication
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
import { GraphQLNonNull, GraphQLInt } from 'graphql';
|
||||
import Credential from '../../models/credential';
|
||||
import authLinkType from '../types/auth-link';
|
||||
import User from '../../models/user';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
|
||||
type Params = {
|
||||
credentialId: number,
|
||||
}
|
||||
const createAuthLinkResolver = async (params: Params) => {
|
||||
const user = await User.query().findOne({
|
||||
email: 'user@automatisch.com'
|
||||
})
|
||||
|
||||
const createAuthLinkResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
const credential = await Credential.query().findOne({
|
||||
user_id: user.id,
|
||||
user_id: req.currentUser.id,
|
||||
id: params.credentialId
|
||||
})
|
||||
|
||||
@@ -29,7 +25,7 @@ const createAuthLink = {
|
||||
args: {
|
||||
credentialId: { type: GraphQLNonNull(GraphQLInt) },
|
||||
},
|
||||
resolve: (_: any, params: Params) => createAuthLinkResolver(params)
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createAuthLinkResolver(params, req)
|
||||
};
|
||||
|
||||
export default createAuthLink;
|
||||
|
@@ -1,24 +1,20 @@
|
||||
import { GraphQLString, GraphQLNonNull, GraphQLObjectType, GraphQLCompositeType } from 'graphql';
|
||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||
import Credential from '../../models/credential';
|
||||
import credentialType from '../types/credential';
|
||||
import twitterCredentialInputType from '../types/twitter-credential-input';
|
||||
import User from '../../models/user';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
|
||||
type Params = {
|
||||
key: string,
|
||||
displayName: string,
|
||||
data: object
|
||||
}
|
||||
const createCredentialResolver = async (params: Params) => {
|
||||
const user = await User.query().findOne({
|
||||
email: 'user@automatisch.com'
|
||||
})
|
||||
|
||||
const createCredentialResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
const credential = await Credential.query().insert({
|
||||
displayName: params.displayName,
|
||||
key: params.key,
|
||||
data: params.data,
|
||||
userId: user.id
|
||||
userId: req.currentUser.id
|
||||
});
|
||||
|
||||
return credential;
|
||||
@@ -31,7 +27,7 @@ const createCredential = {
|
||||
displayName: { type: GraphQLNonNull(GraphQLString) },
|
||||
data: { type: GraphQLNonNull(twitterCredentialInputType) }
|
||||
},
|
||||
resolve: (_: any, params: Params) => createCredentialResolver(params)
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createCredentialResolver(params, req)
|
||||
};
|
||||
|
||||
export default createCredential;
|
||||
|
@@ -1,10 +1,6 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import User from '../models/user';
|
||||
import UserType from '../types/user';
|
||||
|
||||
interface RequestWithCurrentUser extends Request {
|
||||
currentUser: UserType
|
||||
}
|
||||
import RequestWithCurrentUser from '../types/express/request-with-current-user';
|
||||
|
||||
const authentication = async (req: RequestWithCurrentUser, _res: Response, next: NextFunction) => {
|
||||
// We set authentication to use the sample user we created temporarily.
|
||||
|
8
packages/backend/src/types/express/request-with-current-user.d.ts
vendored
Normal file
8
packages/backend/src/types/express/request-with-current-user.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Request } from 'express';
|
||||
import User from '../user';
|
||||
|
||||
interface RequestWithCurrentUser extends Request {
|
||||
currentUser: User
|
||||
}
|
||||
|
||||
export default RequestWithCurrentUser;
|
Reference in New Issue
Block a user