|
| 1 | +import passport from '../config/passport.config.js'; |
| 2 | +import User from '../models/user.model.js'; |
| 3 | +import { generateToken } from '../config/passport.config.js'; |
| 4 | + |
| 5 | +// Redirect to Google OAuth |
| 6 | +export const googleAuth = passport.authenticate('google', { |
| 7 | + scope: ['profile', 'email'] |
| 8 | +}); |
| 9 | + |
| 10 | +// Handle Google OAuth callback |
| 11 | +export const googleCallback = (req, res, next) => { |
| 12 | + passport.authenticate('google', (err, user, info) => { |
| 13 | + if (err) { |
| 14 | + console.error('Google OAuth error:', err); |
| 15 | + return res.status(500).json({ |
| 16 | + success: false, |
| 17 | + message: 'Authentication failed', |
| 18 | + error: err.message |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + if (!user) { |
| 23 | + return res.status(401).json({ |
| 24 | + success: false, |
| 25 | + message: 'Authentication failed' |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + // Generate JWT token |
| 30 | + const token = generateToken(user); |
| 31 | + |
| 32 | + // Return success response with token and user info |
| 33 | + res.json({ |
| 34 | + success: true, |
| 35 | + message: 'Authentication successful', |
| 36 | + token, |
| 37 | + user: { |
| 38 | + id: user._id, |
| 39 | + email: user.email, |
| 40 | + name: user.name, |
| 41 | + firstName: user.firstName, |
| 42 | + lastName: user.lastName, |
| 43 | + profilePicture: user.profilePicture, |
| 44 | + authProvider: user.authProvider, |
| 45 | + lastLogin: user.lastLogin |
| 46 | + } |
| 47 | + }); |
| 48 | + })(req, res, next); |
| 49 | +}; |
| 50 | + |
| 51 | +// Get current user profile |
| 52 | +export const getProfile = async (req, res) => { |
| 53 | + try { |
| 54 | + const user = await User.findById(req.user.userId).select('-__v'); |
| 55 | + |
| 56 | + if (!user) { |
| 57 | + return res.status(404).json({ |
| 58 | + success: false, |
| 59 | + message: 'User not found' |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + res.json({ |
| 64 | + success: true, |
| 65 | + user: { |
| 66 | + id: user._id, |
| 67 | + email: user.email, |
| 68 | + name: user.name, |
| 69 | + firstName: user.firstName, |
| 70 | + lastName: user.lastName, |
| 71 | + profilePicture: user.profilePicture, |
| 72 | + authProvider: user.authProvider, |
| 73 | + isActive: user.isActive, |
| 74 | + createdAt: user.createdAt, |
| 75 | + lastLogin: user.lastLogin |
| 76 | + } |
| 77 | + }); |
| 78 | + } catch (error) { |
| 79 | + console.error('Get profile error:', error); |
| 80 | + res.status(500).json({ |
| 81 | + success: false, |
| 82 | + message: 'Failed to get user profile', |
| 83 | + error: error.message |
| 84 | + }); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +// Logout (invalidate token on client side) |
| 89 | +export const logout = (req, res) => { |
| 90 | + res.json({ |
| 91 | + success: true, |
| 92 | + message: 'Logout successful. Please remove the token from client storage.' |
| 93 | + }); |
| 94 | +}; |
| 95 | + |
| 96 | +// Refresh token |
| 97 | +export const refreshToken = async (req, res) => { |
| 98 | + try { |
| 99 | + const user = await User.findById(req.user.userId); |
| 100 | + |
| 101 | + if (!user) { |
| 102 | + return res.status(404).json({ |
| 103 | + success: false, |
| 104 | + message: 'User not found' |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + // Generate new token |
| 109 | + const token = generateToken(user); |
| 110 | + |
| 111 | + res.json({ |
| 112 | + success: true, |
| 113 | + message: 'Token refreshed successfully', |
| 114 | + token |
| 115 | + }); |
| 116 | + } catch (error) { |
| 117 | + console.error('Refresh token error:', error); |
| 118 | + res.status(500).json({ |
| 119 | + success: false, |
| 120 | + message: 'Failed to refresh token', |
| 121 | + error: error.message |
| 122 | + }); |
| 123 | + } |
| 124 | +}; |
0 commit comments