feat(api): create endpoint to update user profile
This commit is contained in:
		
							parent
							
								
									da0b811428
								
							
						
					
					
						commit
						c86363b766
					
				
							
								
								
									
										38
									
								
								server/api/user/me/profile.put.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								server/api/user/me/profile.put.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | |||||||
|  | import san from "sanitize-html"; | ||||||
|  | import axios from "axios"; | ||||||
|  | import { Profile } from "~/lib/client/types/form/myStuff"; | ||||||
|  | import { apiRoot, h2m } from "~/lib/server/constants"; | ||||||
|  | import forumId from "~/lib/server/forumId"; | ||||||
|  | import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; | ||||||
|  | import { User } from "~/models/user"; | ||||||
|  | 
 | ||||||
|  | export default eventHandler(async (ev) => { | ||||||
|  | 	isLoggedIn(ev); | ||||||
|  | 	const body = await readBody<Profile>(ev); | ||||||
|  | 	const update: any = { | ||||||
|  | 		"profile.occupation": body.occupation, | ||||||
|  | 		"profile.website": body.website, | ||||||
|  | 		"profile.blog": body.blog, | ||||||
|  | 		"profile.bio": san(body.bio), | ||||||
|  | 		"profile.showEmail": !!body.showEmail, | ||||||
|  | 		"profile.avatar": body.avatar, | ||||||
|  | 	}; | ||||||
|  | 	let d = { | ||||||
|  | 		signature: h2m.turndown(body.signature), | ||||||
|  | 		_uid: 1, | ||||||
|  | 	}; | ||||||
|  | 	let lookup = await forumId(ev.context.currentUser!._id!); | ||||||
|  | 	await axios.put(`${apiRoot}/v3/users/${lookup}`, { | ||||||
|  | 		body: d, | ||||||
|  | 		headers: { | ||||||
|  | 			Authorization: `Bearer ${useRuntimeConfig().nodebb.masterToken}`, | ||||||
|  | 		}, | ||||||
|  | 	}); | ||||||
|  | 	await User.findByIdAndUpdate(ev.context.currentUser?._id, { | ||||||
|  | 		$set: update, | ||||||
|  | 	}); | ||||||
|  | 	return { | ||||||
|  | 		success: true, | ||||||
|  | 		message: "Profile updated successfully", | ||||||
|  | 	}; | ||||||
|  | }); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user