Skip to content
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

Checking if login token is valid #96

Open
sgup opened this issue Oct 24, 2019 · 2 comments
Open

Checking if login token is valid #96

sgup opened this issue Oct 24, 2019 · 2 comments

Comments

@sgup
Copy link

sgup commented Oct 24, 2019

Hi, I have a react native app where I'd like to just use the login token if a valid one is available. Is this the correct way?

const token = (await getLoginToken()) || null;

if(token) {
   const tokenExpires = await AsyncStorage.getItem('Meteor.loginTokenExpires');
   const tokenIsValid = new Date() < new Date(tokenExpires)
  
  if(tokenIsValid) // take user to homepage
  else // take user to login or use stored email/pass to loginWithPassword
}
@brightchip
Copy link

Did you find out ?
How do we check the token on server?
Can it use seperated Redis for Token checking

@brightchip
Copy link

brightchip commented Oct 1, 2020

I think i got this:

schema:

` import { gql } from 'apollo-server-express';

export default gql

type Query {
users: [User!]
user(_id: ID!): User

}

extend type Mutation {
updateUser(username: String!): User!

resumeLogin(token:String!,services:VaildSerivesEnumType!): User

}

type Email {
address: String
verified: Boolean
}

type Token {
token: String!
}

type lastlogin{
date:Date
ipAddr:String
}

type UserStatus{
lastlogin:lastlogin
userAgent:String
online:Boolean
page:String
idle:Boolean
}

enum VaildSerivesEnumType{
password
google
}

type User {
id: ID
username: String
displayName: String
password:String
roles: [String!]
heartbeat:Date
hideMe:Boolean
status:UserStatus

registered_emails: [Email]
createdAt:Date

emails: [Email]

picture:String
coverImg:String
description:String
brief:String

}

`

resolver:
`
import { Resolvers } from 'meteor/nicolaslopezj:apollo-accounts/src/Mutation/hashPassword.js'
import { combineResolvers } from 'graphql-resolvers';
import { isAuthenticated } from './authorization';

import { Accounts } from 'meteor/accounts-base';

export default {
Query: {
users: async(parent, args, { models }) => {
return await Meteor.users.find();
},
user: async(parent, { _id }, { models }) => {
return await Meteor.users.findById(_id);
},
},

Mutation: {
    resumeLogin: async(parent, { token, services }, { models }) => {
        const hashedToken = Accounts._hashLoginToken(token);
        let selector = {};

        if ('password' == services) {
            selector['services.resume.loginTokens.hashedToken'] = hashedToken
        } else if ('google' == services) {
            selector['services.google.accessToken'] = hashedToken
        } else {
            return
        }
        // console.log('selector', selector)
        return await Meteor.users.find(selector).fetch()[0];
    },
    ...Resolvers,
    updateUser: combineResolvers(
        isAuthenticated,
        async(parent, { username }, { models, me }) => {
            return await models.User.findByIdAndUpdate(
                me.id, { username }, { new: true },
            );
        },
    ),

},

};And then you can resume login on each request that need auth:
mutation{
resumeLogin(token:"TOKEN",services:password){
id
}
}`

enum:

`
import {
GraphQLEnumType,
} from 'graphql';

const VaildSerivesEnumType = new GraphQLEnumType({
name: 'VAILD_SERIVCES',
values: {
password: {
value: 'password',
},
google: {
value: 'google',
},
},
});
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants