How to use the passport/authorize function in passport

To help you get started, we’ve selected a few passport examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tubackkhoa / tkframework / server / data / graphql / types / mutations / author / update.js View on Github external
mutateAndGetPayload: async ({id, description, introduction, name, avatar, social_accounts}, {request}) => {      
    
    // authorize this request first
    authorize(request)
    
    // insert then return post, we can use try catch instead of error callback
    const authorId = fromGlobalId(id).id    

    const author = await models.authors.findById(authorId, {
      attributes: ['id', 'image'],      
    })

    const authorAttributes = {description, introduction, name}
    if(avatar) {
      // first, delete image of author, then upload image,       
      // should use saveSync, because we have to wait here, sync is always faster if      
      const imagePath = path.join(filePath, 'author/image', authorId)
      // delete at background
      author.image && fse.remove(path.join(imagePath, author.image), err => 
        console.log(err || `Removed file ${author.image}`)
github tubackkhoa / tkframework / server / routes / api / user.js View on Github external
router.put('/block/:id', (req, res) => {
  authorize(req)
  // only admin user can block
  const {id} = req.params
  users.update({
    block: sequelize.literal('NOT `block`'),
  }, {
    where: {id},
  })
  res.send({id})
})
github tubackkhoa / tkframework / server / routes / api / service-point.js View on Github external
router.post('/update', async (req, res) => {
  // check authorize first, for update, also check author_id for post
  authorize(req)
  // currently we not process items, let it for edit phrase
  const {item:{image, ...data}, id} = req.body
  
  data.user_id = req.user.id
  const rad_lat = data.lat * RAD
  const rad_lng = data.lng * RAD
  data.lat_cos = Math.cos(rad_lat)
  data.lng_cos = Math.cos(rad_lng)
  data.lat_sin = Math.sin(rad_lat)
  data.lng_sin = Math.sin(rad_lng)

  // update from post data
  const item = id 
    ? await service_points.findById(id)
    : await service_points.create(data)
github tubackkhoa / tkframework / server / data / graphql / types / mutations / post / add.js View on Github external
mutateAndGetPayload: async ({title}, {request}) => {      
    authorize(request)
    // insert then return post, we can use try catch insteadz
    const post = await models.posts.create({title})
    return {post}
  },
})
github tubackkhoa / tkframework / server / data / graphql / fields / posts.js View on Github external
async (options, resolverAttributes) => {
      
      // only published item for unauthorized users
      // for edit post, also check request.user.id to modify post
      options.where = {}
      if(authorize(request, false)){
        if(args.accepted)
          options.where.accepted = args.accepted
      } else 
        options.where.accepted = 1

      // update options with tag
      if(args.tagId){
        const tagId = fromGlobalId(args.tagId).id
        const postIDs = await models.taggings.findAll({
          attributes: ['subject_id'],
          distinct: true,
          where: {
            tag_id: tagId,
            subject_type: 'Post',
          }
        }).map(tagging => tagging.subject_id)
github tubackkhoa / tkframework / server / data / graphql / types / mutations / post / remove.js View on Github external
mutateAndGetPayload: ({id}, {request}) => {    
    authorize(request)  
    const postId = fromGlobalId(id).id
    models.posts.destroy({
      where: {id: postId}
    })

    // also destroy items, taggings, and all item types belong to this items
    // for item type is item_image, also remove image folder
    models.taggings.destroy({
      where: {
        subject_id:postId,
        subject_type:'Post',
      }
    })

    // now destroy all items
    models.items.findAll({
github tubackkhoa / tkframework / server / data / graphql / types / mutations / post / toggle-acceptance.js View on Github external
mutateAndGetPayload: async ({id}, {request}) => {      
    authorize(request)
    // insert then return post, we can use try catch instead of error callback
    
    // error is good enough
    const postId = fromGlobalId(id).id    
    // need id to map, or we can use literal
    // we can use literal because no need to escape, so we have the best performance
    models.posts.update({
      accepted: models.sequelize.literal('NOT `accepted`'),
    }, {
      where: {id: postId},
    })
    // update async, only save changes
    return {id}
  },
})
github tubackkhoa / tkframework / server / routes / api / news-post.js View on Github external
router.post('/update', async (req, res) => {
  // check authorize first, for update, also check author_id for post
  authorize(req)
  // currently we not process items, let it for edit phrase
  const {item:{image, ...data}, id} = req.body

  // when delete, should delete all images belong to this content
  data.content = data.content.replace(/{
    // replace base 64 with src
    return `
github tubackkhoa / tkframework / server / routes / api / sellpost.js View on Github external
router.post('/update', async (req, res) => {
  // check authorize first, for update, also check author_id for post
  authorize(req)
  // currently we not process items, let it for edit phrase
  const {item:{image, ...data}, id} = req.body  

  // update current user id
  data.user_id = req.user.id
  // update from post data
  const item = id 
    ? await sellposts.findById(id)
    : await sellposts.create(data)

  if(!id)
    res.send({id:item.id})

  uploadImage(image, `sellpost/image/${item.id}`, imagePath => data.image=imagePath)

  // do at background
github tubackkhoa / tkframework / server / routes / shared / utils / mongodb.js View on Github external
export const getDeleteRouter = (model) => (req, res) => {
  authorize(req)
  const {id} = req.params
  model.destroy({
    where:{id}
  })
  .then(deletedNumber => res.send({deletedNumber}))
}