<commit_before>const url = require('url')
const express = require('express')
const passport = require('passport')
const auth = require('../middleware/auth')
const Log = require('../models/logs')

const router = module.exports = express.Router()

router.get('/', (req, res, next) => {
  if (!req.user) {
    if (req.accepts('html')) {
      req.prismic.api.getSingle('sign-in').then(doc => {
        res.locals.title = doc.getStructuredText('sign-in.title').asText()
        res.locals.content['sign-in'] = doc
        res.render('/auth')
      }, next)

      Log.create({
        category: 'Auth',
        type: 'get'
      })
    } else {
      res.status(406).end()
    }
  } else {
    res.redirect(`/cooperatives/${req.user.cooperative}`)
  }
})

router.get('/sign-up', (req, res, next) => {
  if (!req.user) {
    if (req.accepts('html')) {
      req.prismic.api.getSingle('registration').then(doc => {
        res.locals.title = doc.getStructuredText('registration.disclaimer_title').asText()
        res.locals.content.registration = doc
        res.render('/auth/sign-up')
      }, next)

      Log.create({
        category: 'Auth',
        type: 'get'
      })
    } else {
      res.status(406).end()
    }
  } else {
    res.redirect(`/cooperatives/${req.user.cooperative}`)
  }
})

/**
 * Redirect to Metry OAuth
 */

router.get('/metry', passport.authenticate('metry', {
  failureRedirect: '/auth'
}))

/**
 * Sign up
 */

router.get('/metry/sign-up', (req, res) => {
  res.redirect(url.resolve(
    process.env.METRY_ENDPOINT,
    `/id/register/brf?${[
      'response_type=code',
      `redirect_uri=${url.resolve(
        process.env.BRFENERGI_SERVICE_URL,
        'auth/callback'
      )}`,
      'scope=basic%20add_to_open_channels',
      `client_id=${process.env.METRY_CLIENT_ID}`
    ].join('&')}`
  ))
})

/**
 * Handle Metry callback redirect
 */

router.get('/callback', (req, res, next) => {
  passport.authenticate('metry', (err, user) => {
    if (err) {
      res.redirect(url.format({
        pathname: '/auth',
        query: { error: 'METRY_ERROR' }
      }))

      Log.create({
        category: 'Error',
        type: 'auth',
        data: err.message
      })<commit_msg>Add new scope to registration OAuth<commit_after>const url = require('url')
const express = require('express')
const passport = require('passport')
const auth = require('../middleware/auth')
const Log = require('../models/logs')

const router = module.exports = express.Router()

router.get('/', (req, res, next) => {
  if (!req.user) {
    if (req.accepts('html')) {
      req.prismic.api.getSingle('sign-in').then(doc => {
        res.locals.title = doc.getStructuredText('sign-in.title').asText()
        res.locals.content['sign-in'] = doc
        res.render('/auth')
      }, next)

      Log.create({
        category: 'Auth',
        type: 'get'
      })
    } else {
      res.status(406).end()
    }
  } else {
    res.redirect(`/cooperatives/${req.user.cooperative}`)
  }
})

router.get('/sign-up', (req, res, next) => {
  if (!req.user) {
    if (req.accepts('html')) {
      req.prismic.api.getSingle('registration').then(doc => {
        res.locals.title = doc.getStructuredText('registration.disclaimer_title').asText()
        res.locals.content.registration = doc
        res.render('/auth/sign-up')
      }, next)

      Log.create({
        category: 'Auth',
        type: 'get'
      })
    } else {
      res.status(406).end()
    }
  } else {
    res.redirect(`/cooperatives/${req.user.cooperative}`)
  }
})

/**
 * Redirect to Metry OAuth
 */

router.get('/metry', passport.authenticate('metry', {
  failureRedirect: '/auth'
}))

/**
 * Sign up
 */

router.get('/metry/sign-up', (req, res) => {
  res.redirect(url.resolve(
    process.env.METRY_ENDPOINT,
    `/id/register/brf?${[
      'response_type=code',
      `redirect_uri=${url.resolve(
        process.env.BRFENERGI_SERVICE_URL,
        'auth/callback'
      )}`,
      'scope=basic%20add_to_open_channels%20write_meter_location',
      `client_id=${process.env.METRY_CLIENT_ID}`
    ].join('&')}`
  ))
})

/**
 * Handle Metry callback redirect
 */

router.get('/callback', (req, res, next) => {
  passport.authenticate('metry', (err, user) => {
    if (err) {
      res.redirect(url.format({
        pathname: '/auth',
        query: { error: 'METRY_ERROR' }
      }))

      Log.create({
        category: 'Error',
        type: 'auth',
        data: err.message
      })
{'fraction_non_alphanumeric': 0.19121273257117238, 'fraction_numerical': 0.004034969737726967, 'mean_word_length': 2.581059390048154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1955608', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1523, 'n_words': 237}