Skip to content

Feat/add classroom #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/actions/adminActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { GET_USERS_SUCCESS, GET_USERS_FAIL, UPDATE_USER_ROLE_SUCCESS, UPDATE_USER_ROLE_FAIL } from './types';
import api from '../utils/axiosConfig';


// Fetch all users
export const getUsers = () => async (dispatch) => {
try {
const res = await api.get(`${process.env.REACT_APP_BLOCKLY_API}/user/users`)
dispatch({
type: GET_USERS_SUCCESS,
payload: res.data
});
} catch (err) {
dispatch({
type: GET_USERS_FAIL,
payload: err.response.data
});
}
};

// Update user role
export const updateUserRole = (userId, role) => async (dispatch) => {
const body = {
"_id": userId,
"newRole": role
};

try {
const res = await api.put(`${process.env.REACT_APP_BLOCKLY_API}/user/role`, body); // API endpoint to update user role
dispatch({
type: UPDATE_USER_ROLE_SUCCESS,
payload: { userId, role: res.data.newRole } // Assuming response contains updated role
});
} catch (err) {
dispatch({
type: UPDATE_USER_ROLE_FAIL,
payload: err.response ? err.response.data : 'Error updating role'
});
}
};
31 changes: 31 additions & 0 deletions src/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,37 @@ export const logout = () => (dispatch) => {
});
};

// Social login action
export const socialLogin = (token) => (dispatch) => {
console.log("Storing token in localStorage:", token); // Add this for debugging

// Set token in Authorization header for all future requests
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;

// Start loading user data
dispatch({ type: USER_LOADING });

// Save the token in localStorage
localStorage.setItem("token", token);

// Fetch user data from the backend
axios
.get("http://localhost:8080/user/me") // Endpoint to fetch user data
.then((res) => {
dispatch({
type: LOGIN_SUCCESS,
payload: {
user: res.data, // User data from the response
token, // Store the token
},
});
})
.catch((err) => {
dispatch({ type: AUTH_ERROR });
console.error("Error loading user data", err);
});
};

export const authInterceptor = () => (dispatch, getState) => {
// Add a request interceptor
axios.interceptors.request.use(
Expand Down
Loading
Loading