-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from NEM-NE/dev
์ฑํ ๋ฐฉ ๋ฒ๊ทธ ์ด์ ๋ฐ ํ๋กํ ์ฌ์ง ๋ณ๊ฒฝ ๋ชจ๋ฌ UI ์์
- Loading branch information
Showing
13 changed files
with
479 additions
and
66 deletions.
There are no files selected for viewing
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 was deleted.
Oops, something went wrong.
This file was deleted.
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,101 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import Users, { IActivity } from '@models/users'; | ||
import Events from '@models/events'; | ||
import { activeUser } from '@src/sockets/user'; | ||
import { userNamespace } from '@src/sockets'; | ||
|
||
let instance: any = null; | ||
class UserService { | ||
constructor() { | ||
if (instance) return instance; | ||
instance = this; | ||
} | ||
|
||
async getActivityList(userDocumentId: string, count: number) { | ||
const user = await Users.findById(userDocumentId, ['activity']); | ||
const newActivityList = await Promise.all(user!.activity.slice(count, count + 10).map(async (activity: IActivity) => { | ||
const detailFrom = await this.findUserByDocumentId(activity.from); | ||
const newFrom = { userId: detailFrom!.userId, userName: detailFrom!.userName, profileUrl: detailFrom!.profileUrl }; | ||
return { ...activity, from: newFrom }; | ||
})); | ||
await Users.findByIdAndUpdate(userDocumentId, { activity: user!.activity.map((el) => ({ ...el, isChecked: true })) }); | ||
return newActivityList; | ||
} | ||
|
||
async isActivityChecked(userDocumentId: string) { | ||
try { | ||
const { activity } : any = await Users.findOne({ _id: userDocumentId }, ['activity']); | ||
if (!activity) return false; | ||
return activity.some((item: IActivity) => !item.isChecked); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
async addActivityTypeFollow(userDocumentId: string, targetUserDocumentId: string) { | ||
try { | ||
const newActivity = { | ||
type: 'follow', | ||
clickDocumentId: userDocumentId, | ||
from: userDocumentId, | ||
date: new Date(), | ||
isChecked: false, | ||
}; | ||
await Users.findByIdAndUpdate(targetUserDocumentId, { $push: { activity: { $each: [newActivity], $position: 0 } } }); | ||
this.emitToUserGetActivity(targetUserDocumentId); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
async addActivityTypeRoom(userDocumentId: string, roomDocumentId: string) { | ||
try { | ||
const user = await Users.findById(userDocumentId, ['followers']); | ||
const newActivity = { | ||
type: 'room', | ||
clickDocumentId: String(roomDocumentId), | ||
from: userDocumentId, | ||
date: new Date(), | ||
isChecked: false, | ||
}; | ||
await Promise.all(user!.followers.map(async (userDocId: string) => { | ||
await Users.findByIdAndUpdate(userDocId, { $push: { activity: { $each: [newActivity], $position: 0 } } }); | ||
this.emitToUserGetActivity(userDocId); | ||
return true; | ||
})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
async addActivityTypeEvent(userDocumentId: string, eventDocumentId: string) { | ||
try { | ||
const event = await Events.findById(eventDocumentId, ['participants']); | ||
const newActivity = { | ||
type: 'event', | ||
clickDocumentId: String(eventDocumentId), | ||
from: userDocumentId, | ||
date: new Date(), | ||
isChecked: false, | ||
}; | ||
await Promise.all(event!.participants.map(async (userId: string) => { | ||
const user = await Users.findOneAndUpdate({ userId }, { $push: { activity: { $each: [newActivity], $position: 0 } } }); | ||
const userDocId = user!._id; | ||
this.emitToUserGetActivity(String(userDocId)); | ||
return true; | ||
})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
emitToUserGetActivity(userDocumentId: string) { | ||
const socketUser = activeUser.get(userDocumentId); | ||
if (socketUser) userNamespace.to(socketUser.socketId).emit('user:getActivity'); | ||
} | ||
} | ||
|
||
export default new UserService(); |
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,120 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import jwt from 'jsonwebtoken'; | ||
|
||
import Users from '@models/users'; | ||
import RefreshTokens from '@models/refresh-token'; | ||
import jwtUtils from '@utils/jwt-util'; | ||
|
||
interface ISignupUserInfo { | ||
loginType: string, | ||
userId: string, | ||
password: string, | ||
userName: string, | ||
userEmail: string, | ||
} | ||
|
||
let instance: any = null; | ||
class AuthService { | ||
constructor() { | ||
if (instance) return instance; | ||
instance = this; | ||
} | ||
|
||
async signIn(email: string, password: string) { | ||
const user = await Users.findOne({ userEmail: email }); | ||
|
||
if (!user) { | ||
return { ok: false, msg: 'there is no user' }; | ||
} | ||
|
||
const isMatch = user.checkPassword(password); | ||
|
||
if (isMatch) { | ||
const accessToken = jwtUtils.sign(user._id, user.userEmail); | ||
const refreshToken = jwtUtils.refresh(); | ||
|
||
const existingRefreshToken = await RefreshTokens.findOneAndUpdate( | ||
{ userDocumentId: user._id }, | ||
{ token: refreshToken }, | ||
); | ||
|
||
if (!existingRefreshToken) { | ||
const usersRefreshTokens = new RefreshTokens({ | ||
userDocumentId: user._id, | ||
token: refreshToken, | ||
}); | ||
await usersRefreshTokens.save(); | ||
} | ||
|
||
return { | ||
accessToken, | ||
ok: true, | ||
msg: 'ok', | ||
}; | ||
} | ||
return { ok: false, msg: 'wrong password' }; | ||
} | ||
|
||
async signup(info: ISignupUserInfo) { | ||
try { | ||
await Users.insertMany([info]); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
async verifyAccessToken(token: string) { | ||
const result = jwtUtils.verify(token); | ||
if (!result.ok) { | ||
const newToken = await this.tokenRefresh(token); | ||
return newToken; | ||
} | ||
return result; | ||
} | ||
|
||
async getRefreshTokens(userDocumentId: string) { | ||
const refreshToken = await RefreshTokens.findOne({ userDocumentId }); | ||
if (!refreshToken) { | ||
return null; | ||
} | ||
return refreshToken.token; | ||
} | ||
|
||
async tokenRefresh(accessToken: string) { | ||
const accessResult = jwtUtils.verify(accessToken); | ||
const decoded = jwt.decode(accessToken) as jwt.JwtPayload; | ||
|
||
if (decoded === null) { | ||
return { | ||
ok: false, | ||
msg: 'No authorized!', | ||
}; | ||
} | ||
|
||
const refreshToken = (await this.getRefreshTokens(decoded.id)) as string; | ||
const refreshResult = await jwtUtils.refreshVerify(refreshToken); | ||
|
||
if (accessResult.ok === false && accessResult.message === 'jwt expired') { | ||
if (refreshResult === false) { | ||
return { | ||
ok: false, | ||
msg: 'No authorized!', | ||
}; | ||
} | ||
const newAccessToken = jwtUtils.sign(decoded.id, decoded.email); | ||
|
||
return { | ||
ok: true, | ||
accessToken: newAccessToken, | ||
userDocumentId: decoded.id, | ||
}; | ||
} | ||
|
||
return { | ||
ok: true, | ||
msg: 'Acess token is not expired!', | ||
}; | ||
} | ||
} | ||
|
||
export default new AuthService(); |
Oops, something went wrong.