How to use deleteUserAccessToken method in devicefarmer-stf

Best JavaScript code snippet using devicefarmer-stf

user.ts

Source:user.ts Github

copy

Full Screen

...14 ...getUserInfo()15}16const setUserDetails = createAsyncThunk('get/userDetails', async (user: AuthUserDetails, { dispatch, rejectWithValue }) => {17 // delete the token incase it user refresh fails then18 deleteUserAccessToken()(dispatch)19 try {20 const request = await fetch(`${process.env.REACT_APP_API_URL}/me`, {21 headers: {22 'Content-Type': 'application/json',23 Authorization: `Bearer ${user.access_token}`24 }25 })26 if (request.status !== 200) {27 const message = (await request.json()).error.message28 return rejectWithValue(message)29 }30 // set token when fetch is successful31 setUserAccessToken(user)(dispatch)32 const { id, display_name, images } = await request.json()33 const userInfo = { id, display_name, images }34 let library = await libraryService.getOrCreateLibrary(id)35 if (!library) {36 library = []37 }38 localStorage.setItem(LOCAL_STORAGE_USER, JSON.stringify(userInfo))39 return { ...userInfo, library }40 } catch (error) {41 return rejectWithValue('Opps there seems to be an error')42 }43})44// ---Adding and removing firestore library--45const setUserLibrary = createAsyncThunk<string[], never, {state: {user: User}}>('set/library', async (_, { getState, rejectWithValue }) => {46 try {47 let library = await libraryService.getOrCreateLibrary(getState().user.id!)48 if (!library) {49 library = []50 }51 return library52 } catch (error) {53 rejectWithValue(`error occured${error.message}`)54 }55})56const addToLibrary = createAsyncThunk<any, string, {state: {user: User}}>('add/library', async (songId: string, { getState, rejectWithValue }) => {57 try {58 await libraryService.addToLibrary(getState().user.id!, songId)59 return songId60 } catch (error) {61 rejectWithValue(`error occured${error.message}`)62 }63})64const removeFromLibrary = createAsyncThunk<any, string, {state: {user: User}}>('remove/library', async (songId: string, { getState, rejectWithValue }) => {65 try {66 await libraryService.removeFromLibrary(getState().user.id!, songId)67 return songId68 } catch (error) {69 rejectWithValue(`error occured${error.message}`)70 }71})72export const userSlice = createSlice({73 name: 'user',74 initialState,75 reducers: {76 setAuthState: (state, action: PayloadAction<string>) => {77 state.authStateCode = action.payload78 },79 setUserToken: (state, action: PayloadAction<Pick<AuthUserDetails, 'access_token'> | null>) => {80 state.token = (action.payload != null) ? action.payload.access_token : null81 },82 setAppError: (state, action: PayloadAction<string | null>) => {83 state.error = action.payload84 },85 resetState: (state) => {86 return {} as User87 },88 setLibrary: (state, action: PayloadAction<string[]>) => {89 state.library = action.payload90 }91 },92 extraReducers: {93 [setUserDetails.rejected.type]: (state, action) => {94 state.error = action.payload95 },96 [setUserDetails.fulfilled.type]: (state, action: PayloadAction<Pick<User, 'display_name'|'id'|'images'>>) => {97 return { ...state, ...action.payload }98 },99 [setUserLibrary.fulfilled.type]: (state, action: PayloadAction<string[]>) => {100 state.library = action.payload101 },102 [setUserLibrary.rejected.type]: (state, action) => {103 state.error = action.payload104 },105 [addToLibrary.fulfilled.type]: (state, action: PayloadAction<string>) => {106 state.library.push(action.payload)107 },108 [addToLibrary.rejected.type]: (state, action) => {109 state.error = action.payload110 },111 [removeFromLibrary.fulfilled.type]: (state, action: PayloadAction<string>) => {112 state.library = state.library.slice().filter(id => id !== action.payload)113 },114 [removeFromLibrary.rejected.type]: (state, action: PayloadAction<string>) => {115 state.error = action.payload116 }117 }118})119const { setAuthState, setUserToken, setAppError, resetState } = userSlice.actions120export { setUserDetails, setAppError, setUserLibrary, addToLibrary, removeFromLibrary }121// add code to local storage without side effect in reducer122export const setAuthStateCode = (code: string) => (dispatch) => {123 localStorage.setItem(LOCAL_STORAGE_AUTH_STATE_CODE, code)124 dispatch(setAuthState(code))125}126export const setUserAccessToken = (user: AuthUserDetails) => (dispatch) => {127 localStorage.setItem(LOCAL_STORAGE_TOKEN, user.access_token)128 dispatch(setUserToken(user))129}130export const deleteUserAccessToken = () => (dispatch) => {131 localStorage.removeItem(LOCAL_STORAGE_TOKEN)132 dispatch(setUserToken(null))133}134export const logoutUser = () => (dispatch) => {135 deleteUserAccessToken()(dispatch)136 dispatch(resetState())137 localStorage.clear()138}...

Full Screen

Full Screen

auth.ts

Source:auth.ts Github

copy

Full Screen

...75 const { username } = await getUserFromUserAccessToken({76 token: authorization,77 })78 await addUserAccessToken({ username, token })79 setTimeout(() => deleteUserAccessToken({ token: authorization }), 3000)80 // 6. Attach new cookie to res81 res.cookie('Authorization', token)82 res.locals.updatedToken = token83 logger.debug('Added new token', token)84 next()85 } catch (error) {86 const errorStatusCode =87 error.message === NO_TOKEN_AUTH_ERROR ||88 error.message === INVALID_TOKEN_AUTH_ERROR ||89 error.message === EXPIRED_TOKEN_ERROR90 ? 40191 : 50092 logger.error('Cannot validate token.', error.stack)93 res.status(errorStatusCode).json(generateGenericErrorResponse(error))94 }95}96export const checkDeleteAuthToken: (97 req: Request,98 res: Response,99 next: NextFunction100) => void = async (req, res, next) => {101 try {102 const authorization: string = req.cookies.Authorization103 // 1. Check token exists.104 if (!authorization) {105 throw new Error(NO_TOKEN_AUTH_ERROR)106 }107 // 2. Check token exists in DB.108 const existingToken: boolean = await doesUserAccessTokenExist({109 token: authorization,110 })111 if (!existingToken) {112 throw new Error(INVALID_TOKEN_AUTH_ERROR)113 }114 // 3. Check JWT properly signed.115 jwt.verify(authorization, key)116 // 4. Remove old token from token array117 await deleteUserAccessToken({118 token: authorization,119 })120 next()121 } catch (error) {122 const errorStatusCode =123 error.message === NO_TOKEN_AUTH_ERROR ||124 error.message === INVALID_TOKEN_AUTH_ERROR ||125 error.message === EXPIRED_TOKEN_ERROR126 ? 401127 : 500128 logger.error('Cannot validate token.', error.stack)129 res.status(errorStatusCode).json(generateGenericErrorResponse(error))130 }131}

Full Screen

Full Screen

user.js

Source:user.js Github

copy

Full Screen

...12}13export function editUser(params) {14 return axios.post('/api/user/edit', params)15}16export function deleteUserAccessToken(params) {17 console.log('deleteUserAccessToken')18 return axios.post('/api/oauth/delete/' + params)19}20axios.defaults.headers.common['Authorization'] = getCookie("token")21// request拦截器22axios.interceptors.request.use(23 config => {24 console.log('getCookie:', getCookie("token"))25 if (getCookie("token") != undefined) {26 config.headers.Authorization = getCookie("token") // 让每个请求携带自定义token 请根据实际情况自行修改27 }28 return config29 },30 error => {...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf-client');2client.deleteUserAccessToken('userAccessToken')3.then(function() {4console.log('User access token deleted');5})6.catch(function(err) {7console.error('Something went wrong:', err.stack);8});9var stf = require('devicefarmer-stf-client');10client.deleteUser('username')11.then(function() {12console.log('User deleted');13})14.catch(function(err) {15console.error('Something went wrong:', err.stack);16});17var stf = require('devicefarmer-stf-client');18client.deleteDevice('serialNumber')19.then(function() {20console.log('Device deleted');21})22.catch(function(err) {23console.error('Something went wrong:', err.stack);24});25var stf = require('devicefarmer-stf-client');26client.deleteGroup('groupName')27.then(function() {28console.log('Group deleted');29})30.catch(function(err) {31console.error('Something went wrong:', err.stack);32});33var stf = require('devicefarmer-stf-client');34client.deleteApp('appId')35.then(function() {36console.log('App deleted');37})38.catch(function(err) {39console.error('Something went wrong:', err.stack);40});41var stf = require('devicefarmer-stf-client');42client.deleteRemoteConnect('remoteConnectId')43.then(function() {44console.log('Remote connect deleted');45})46.catch(function(err) {47console.error('Something went wrong:', err.stack);48});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run devicefarmer-stf automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful